循环,想要在循环结束时打印出有关字典对象数组的信息
这可能是几个问题捆绑在一起的结果,但情况是这样的。我正在 Objective C 中工作,并且有一个 for 循环,它会遍历一系列字典对象......就像这样。
for (NSDictionary *dictionary in array){
log(@"the dictionary object looks like this \n%@\n",dictionary);
log(@"value of someProp in the current dict object is %@",[dictionary valueForKey:@"someProp"]);
}
好吧,这一切都很好,但我想稍微整理一下它如何打印到控制台。我是 Objective C 的新手,但在其他语言方面经验丰富。我想在 Objective C 中做这样的事情(例如,用 ActionScript 编写),
var outputString:String;
for (items in dictionary){
outputString += dictionary.toString();
outputString += "value of item in current dict object is " + dictionary [item];// etc
}
// now just print out the contents of outputString;
trace(outputString);
我在这里遇到的挑战是整理不是字符串或 NSString 类型的对象。
我知道类似的东西
NSString *outputString = @"";
for (//same loop as above in obj-c){
outputString = [outputString stringByAppendingString:**someString**];
}
,但也许我只是不熟悉适当的字符串方法,因为简单地替换 dictionary 来打印它读取 someString 的整个字典对象会产生错误,因为字典对象当然不是字符串。
所以我想
- 累积我想要 NSLog 输出的一系列字符串的内容,将其全部总结
- 这包括打印出字典对象(或者也可能是数组对象)的内容,将其转换为字符串,并将其添加到outputString。
有人对如何去做这件事有什么建议吗?
This may be a few questions bundled into one, but here is the situation. I am working in objective c, and have a for loop that's going through an array of dictionary objects... like so.
for (NSDictionary *dictionary in array){
log(@"the dictionary object looks like this \n%@\n",dictionary);
log(@"value of someProp in the current dict object is %@",[dictionary valueForKey:@"someProp"]);
}
well that's all nice and dandy but I want to tidy up how it prints out to the console a little bit. I am new to objective c, but experienced in other languages. I want to do something like this in objective c (wrote it in actionscript for an example)
var outputString:String;
for (items in dictionary){
outputString += dictionary.toString();
outputString += "value of item in current dict object is " + dictionary [item];// etc
}
// now just print out the contents of outputString;
trace(outputString);
the challenge I have here is collating the objects that are not of type string or NSString..
I know stuff like
NSString *outputString = @"";
for (//same loop as above in obj-c){
outputString = [outputString stringByAppendingString:**someString**];
}
but maybe I am just unfamiliar with the appropriate string method, because simply substituting dictionary to print out the whole dictionary object where it reads someString will produce an error because of course a dictionary object is not a string.
So I would like to
-accumulate the contents of what I would like to NSLog out in a series of strings summing it all up
-this includes the printing out the contents of dictionary objects (or maybe array objects too) converting it to a string, and adding it to outputString.
Anyone have any advice on how to go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过以下方式编写 NSDictionary 的序列化版本...
假设您的 NSDictionary 实例名为
dictionaryItem
。它将输出类似于...的字典对象
You can write a serialized version of the NSDictionary by...
assuming your instance of NSDictionary is named
dictionaryItem
.It will output the dictionary object similar to...
尝试使用 [NSString stringWithFormat:@"Something about value: %@",dictionaryItem]
Try using [NSString stringWithFormat:@"Something about value: %@", dictionaryItem]