由于未捕获的异常“NSRangeException”而终止应用程序给出范围或索引越界'
我正在开发条形码应用程序并从 CLController 获取位置,因此它给出位置、速度和许多其他信息,因此我对 WithRange 进行子串并得到异常,所以请给我原因,我应该为此做什么?
提前致谢。
- (void)locationUpdate:(CLLocation *)location {
locstr = [location description ];
//NSLog(@"current location is %@ ",locstr);
NSString* regexString =@"<(.*?)>";
NSRegularExpressionOptions options = NSRegularExpressionCaseInsensitive;
NSError* error = NULL;
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:regexString options:options error:&error];
if (error) {
//NSLog(@"error is %@", [error description]);
}
NSArray* results = [regex matchesInString:locstr options:0 range:NSMakeRange(1, [locstr length])];
for (NSTextCheckingResult* result in results) {
resultString = [locstr substringWithRange:result.range];
//NSLog(@"%@",resultString);
//life = [[resultString substringWithRange:NSMakeRange(1)] retain];
resultString =[resultString substringWithRange:NSMakeRange(1,27)];
resultString = [resultString stringByReplacingOccurrencesOfString:@" "
withString:@""];
life = [resultString stringByReplacingOccurrencesOfString:@"+"
withString:@""];
life = [[life substringWithRange:NSMakeRange(0,[life length]-1)] retain];
//NSLog(@"in update %@",life);
}
得到
这个异常
2011-11-23 14:24:58.161 BarCodeApp[2632:707] * 终止应用程序 未捕获异常'NSRangeException',原因:' -[NSRegularExpression enumerateMatchesInString:options:range:usingBlock:]:范围或索引 越界'第一次抛出调用堆栈:(0x326398bf 0x364a31e5 0x326397b9 0x326397db 0x3783846b 0x37838a11 0x37af 0xbe73 0x3548f5df 0x3548ef81 0x3548962f 0x3260db31 0x3260d15f 0x3260c381 0x3258f4dd 0x3258f3a5 0x37cd0fed 0x30eda743 0x2337 0x22b8) 终止调用 抛出异常程序收到信号:“SIGABRT”。数据 格式化程序暂时不可用,将在“继续”后重试。 (找不到dlopen函数,所以无法加载共享 图书馆。)
I am working on barcode app and getting the location from CLController so its giving location,speed and many other thing so i substring that WithRange and getting exception so please kindly give me the reason and what should i do for this?
Thanks in advance.
- (void)locationUpdate:(CLLocation *)location {
locstr = [location description ];
//NSLog(@"current location is %@ ",locstr);
NSString* regexString =@"<(.*?)>";
NSRegularExpressionOptions options = NSRegularExpressionCaseInsensitive;
NSError* error = NULL;
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:regexString options:options error:&error];
if (error) {
//NSLog(@"error is %@", [error description]);
}
NSArray* results = [regex matchesInString:locstr options:0 range:NSMakeRange(1, [locstr length])];
for (NSTextCheckingResult* result in results) {
resultString = [locstr substringWithRange:result.range];
//NSLog(@"%@",resultString);
//life = [[resultString substringWithRange:NSMakeRange(1)] retain];
resultString =[resultString substringWithRange:NSMakeRange(1,27)];
resultString = [resultString stringByReplacingOccurrencesOfString:@" "
withString:@""];
life = [resultString stringByReplacingOccurrencesOfString:@"+"
withString:@""];
life = [[life substringWithRange:NSMakeRange(0,[life length]-1)] retain];
//NSLog(@"in update %@",life);
}
}
getting this exception
2011-11-23 14:24:58.161 BarCodeApp[2632:707] * Terminating app due
to uncaught exception 'NSRangeException', reason: '
-[NSRegularExpression enumerateMatchesInString:options:range:usingBlock:]: Range or index
out of bounds' First throw call stack: (0x326398bf 0x364a31e5
0x326397b9 0x326397db 0x3783846b 0x37838a11 0x37af 0xbe73 0x3548f5df
0x3548ef81 0x3548962f 0x3260db31 0x3260d15f 0x3260c381 0x3258f4dd
0x3258f3a5 0x37cd0fed 0x30eda743 0x2337 0x22b8) terminate called
throwing an exceptionProgram received signal: “SIGABRT”. Data
Formatters temporarily unavailable, will re-try after a 'continue'.
(Can't find dlopen function, so it is not possible to load shared
libraries.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
dito Aadhira
应用程序也可能崩溃于
You Define a range from 1 to [locstr length] there while [locstr length] must be [locstr length]-1.
字符串“NSMakeRange”的长度为 11 个字符。所以,它的数组索引范围是0-10。覆盖您的代码,范围将为 1-11,因为您从 1 开始,到 1+10=11 停止。
dito Aadhira
The app could also crash at
You define a range from 1 to [locstr length] there while [locstr length] must be [locstr length]-1.
The string "NSMakeRange" has a length of 11 characters. So, its array index range is is 0-10. Covering your code, the range would be 1-11 as you start at 1 and stop at 1+10=11.
您确定
resultString
长度大于 27 吗?无论如何,如果它小于 27,它就会崩溃。看来应用程序在那里崩溃了。可能在其他一些地方您也使用过 NSRange。启用 NSZombie 来获取应用程序崩溃的确切位置。
Are you sure the
resultString
length is greater than 27? By any means if it is less than 27, it will crash. It seems the app crashes there. May be in some other places also where you have used NSRange.Enable NSZombie to get where exactly the app crashes.
为什么要解析
[CLLocation description]
而不是使用位置、准确性等属性?描述不应该这样使用。Why you're parsing the
[CLLocation description]
instead of using the properties, like location, accuracy, etc? The description is not supposed to be used like this.