循环时,为 a 中的每个字母创建一个子字符串,NSString
抛出此异常:
-[__NSCFString substringWithRange:]: Range or index out of bounds
代码:
- (void) analyzeContent:(NSString *)content
{
content = [content uppercaseString];
for (int i = 1; i < content.length; i++) {
NSString *ch = [content substringWithRange:NSMakeRange(i-1, i)]; // <-- exception
}
}
从这里调用:
- (IBAction)analyze:(id)sender
{
if (!self.text) {
NSString *path = [[NSBundle mainBundle] pathForResource:CYPHERTEXT_FILE
ofType:@"txt"];
self.text = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
}
[self.progIndicator setHidden:NO];
[self.progIndicator startAnimation:nil];
dispatch_queue_t queue = dispatch_queue_create("queue", 0);
dispatch_async(queue, ^{
[self.analyzer analyzeContent:self.text];
// When finished update UI
dispatch_sync(dispatch_get_main_queue(), ^{
[self.progIndicator setHidden:YES];
[self.progIndicator stopAnimation:nil];
});
});
}
text
- 财产声明是:@property (copy) NSString *text;
当i = 13172
和时抛出异常content.length = 26344
。
该代码在后台线程中运行,但content
不被任何其他线程访问。
感谢您的任何帮助!