我有一个核心数据对象NSMutableArray
。我想创建一个NSString
这些对象以这种方式格式化:
关键字 - 定义
关键字 - 定义
关键字 - 定义
我有以下代码从数组中收集一个索引:
Term *term = [self.terms objectAtIndex:1];
NSString *string = [NSString stringWithFormat:@"%@ - %@ \r\n", term.keyword, term.definition];
我不确定如何循环这个,以便数组中的每个索引都被添加到字符串中。提前谢谢!
答案 0 :(得分:1)
NSMutableString *string = [NSMutableString string];
for (Term *term in self.terms)
{
[string appendFormat: @"%@ - %@\r\n", term.keyword, term.definition];
}
//at this point "string" contains keywords and definitions of all terms
答案 1 :(得分:0)
使用此,
for(Term *term in self.terms)
{
NSString *string = [NSString stringWithFormat:@"%@ - %@ \r\n", term.keyword, term.definition];
}
答案 2 :(得分:0)
for (Term *term in self.terms) {
// Note: This string is only visible in the scope of the loop, define it elsewhere if you wish to do something with the string outside of the loop
NSString *string = [NSString stringWithFormat:@"%@ - %@ \r\n", term.keyword, term.definition];
// Do something with string.
}