1

我正在开发条形码应用程序并从 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:]: Range or index out of bounds”第一掷调用堆栈:(0x326398bf 0x364a31e5 0x326397b9 0x326397db 0x3783846b 0x37838a11 0x37af 0xbe73 0x3548f5df 0x3548ef81 0x3548962f 0x3260db31 0x3260d15f 0x3260c381 0x3258f4dd 0x3258f3a5 0x37cd0fed 0x30eda743 0x2337 0x22b8)终止叫做抛出exceptionProgram接收到的信号:“SIGABRT”。数据格式化程序暂时不可用,将在“继续”后重试。(找不到 dlopen 函数,所以无法加载共享库。)

4

3 回答 3

2

迪托·阿迪拉

该应用程序也可能在

NSMakeRange(1, [locstr length])

您在那里定义从 1 到 [locstr length] 的范围,而 [locstr length] 必须是 [locstr length]-1。

字符串“NSMakeRange”的长度为 11 个字符。所以,它的数组索引范围是0-10。覆盖您的代码,范围将是 1-11,因为您从 1 开始并在 1+10=11 停止。

于 2011-11-23T10:29:43.223 回答
1
resultString =[resultString substringWithRange:NSMakeRange(1,27)];

你确定resultString长度大于27吗?无论如何,如果它小于 27,它将崩溃。似乎应用程序在那里崩溃了。可能在您使用过 NSRange 的其他一些地方。

启用 NSZombie 以获取应用程序崩溃的确切位置。

于 2011-11-23T10:00:12.877 回答
1

为什么要解析[CLLocation description]而不是使用属性,例如位置、准确性等?描述不应该这样使用。

于 2011-11-23T10:00:37.330 回答