0

我有一个NSMutableArray核心数据对象。我想创建一个NSString以这种方式格式化的这些对象:

关键字 - 定义

关键字 - 定义

关键字 - 定义

我有以下代码从数组中收集一个索引:

Term *term = [self.terms objectAtIndex:1];
NSString *string = [NSString stringWithFormat:@"%@ - %@ \r\n", term.keyword, term.definition];

我不确定如何循环它,以便将数组中的每个索引都添加到字符串中。提前致谢!

4

3 回答 3

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
于 2013-10-23T09:26:47.453 回答
0

用这个,

for(Term *term in self.terms)
 {
    NSString *string = [NSString stringWithFormat:@"%@ - %@ \r\n", term.keyword, term.definition];
  }
于 2013-10-23T09:23:49.173 回答
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.
}
于 2013-10-23T09:24:42.620 回答