为什么我无法将appendString添加到for循环中?
问题描述:
我有下面的代码,我试图简单地循环通过一个实体,并为每个NSPropertyDescription名称,将它追加到一个可变的字符串,我将建立一个进一步的字符串。为什么我无法将appendString添加到for循环中?
- (void) createCSV {
NSEntityDescription *anEntity = [NSEntityDescription entityForName:@"Missions" inManagedObjectContext:self.missionDatabase.managedObjectContext];
NSMutableString *csvString = [NSString string];
for (NSPropertyDescription *property in anEntity) {
[csvString appendString:property.name];
}
NSLog(@"%@",csvString);
}
运行下面的代码时的问题是,我得到一个
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with appendString:'
我想不出什么我做错了。我能否将NSString追加到NSMutableString?
答
再看一遍。 csvString
不可变。您正在创建NSString
。创建一个NSMutableString
看起来像这样:
NSMutableString *csvString = [NSMutableString string];
这就是一个新的眼球的美丽。我感谢帮助,并感到愚蠢的看不到现在。 – Russ 2012-08-15 02:34:09
它发生..... – 2012-08-15 02:34:52