这个适用于ios模拟器但不适用于我的iphone设备。为什么呢?
NSMutableString *categoryList = [NSMutableString string];
for (NSArray *category in self.storedCategories) {
[categoryList appendString:[category valueForKey:@"value"]];
[categoryList appendString:@","];
}
[categoryList deleteCharactersInRange:NSMakeRange([categoryList length]-1, 1)]
2014-01-17 19:41:58.572 someapp[14586:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString deleteCharactersInRange:]: Range or index out of bounds'
*** First throw call stack:
(0x2de38f4b 0x382796af 0x2de38e8d 0x2ddfd88f 0x98419 0x305e0713 0x305e06b3 0x305e0691 0x305cc11f 0x305e0107 0x305dfdd9 0x305dae65 0x305b079d 0x305aefa3 0x2de04183 0x2de03653 0x2de01e47 0x2dd6cc27 0x2dd6ca0b 0x32a93283 0x30610049 0x995ed 0x38781ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
答案 0 :(得分:2)
如果self.storedCategories
为空或空,那么您将尝试从空字符串中删除字符。在修剪/删除之前,您应该始终检查字符串长度。
答案 1 :(得分:1)
您的应用程序在实际设备上而不是在模拟器上崩溃似乎很有趣。我建议你实际上通过替换
来重新设计你的实现[categoryList appendString:@","];
用这个:
if(![[self.storedCategories lastObject] isEqual:category]) {
[categoryList appendString:@","];
}
这将阻止在末尾添加逗号(这是我假设你正在尝试使用deleteCharactersInRange:方法),它还将处理自身中没有对象的情况.storedCategories,你试图从空字符串中删除一个字符!