Ios 将NSDictionary中的所有内容放入UITextView
我得到了一个Ios 将NSDictionary中的所有内容放入UITextView,ios,objective-c,nsdictionary,Ios,Objective C,Nsdictionary,我得到了一个NSDictionary,我想把它打印到UITextView中 我尝试使用此代码,但当我尝试将其放入UITextView时,它只得到一行文本,而当我NSLog时,它工作得非常好 for(int i=0; i<[array count];i++) { dictionary = [array objectAtIndex:i]; //that shows only one line from the dictionary in the textview tex
NSDictionary
,我想把它打印到UITextView
中
我尝试使用此代码,但当我尝试将其放入UITextView
时,它只得到一行文本,而当我NSLog
时,它工作得非常好
for(int i=0; i<[array count];i++) {
dictionary = [array objectAtIndex:i];
//that shows only one line from the dictionary in the textview
textview.text = [dictionary objectForKey:@"text"];
//that shows all the dictionary with key "text" in the log
NSLog(@"%@", [dictionary objectForKey:@"text"]);
}
for(int i=0;i您应该将其附加到现有文本中
textview.text = @"";
for(int i=0; i<[array count];i++)
{
dictionary= [array objectAtIndex:i];
//that shows only one line from the dictionary in the textview
textview.text = [textview.text stringByAppendingFormat:@"%@\n",[diction objectForKey:@"text"]]];
//that shows all the dictionary with key "text" in the log
NSLog(@"%@",[diction objectForKey:@"text"]);
}
textview.text=@”;
对于(int i=0;i而言,问题在于textview.text
正在覆盖textview.text
的先前内容
您需要做的是将字符串附加到textview
的现有值:
textview.text=[textview.text stringByAppendingString:[DictionObjectForkey:@“text”]];
但这将同时获得所有值,因此您可能希望在值之间添加一个空格:
NSString*appendedText=[nsstringstringwithformat:@“%@%@”,textview.text,[diction objectForKey:@“text”];
textview.text=附录文本;
最后,您可以使用现代Objective-C文本访问字典对象,如array[i]
和diction[@“text”]
而不是使用objectAtIndex
和objectForKey
来减少键入量。当您设置textview时。text
覆盖该属性之前的任何设置。因此,您只能看到循环中设置的最后一个。为什么要使用两个格式化程序?为什么不只是textview.text=[textview.text stringByAppendingFormat:@“%@\n”,措辞[@“text]”;
?更新后,我更习惯于创建NSString stringWithFormat
。但在检查时,我刚刚复制了appendingFormat,以前使用AppendingStringGi,我怀疑OP希望将“text”值放在单独的行上。