1

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.)

Reno
  • 33,594
  • 11
  • 89
  • 102
Furqi
  • 2,403
  • 1
  • 26
  • 32

3 Answers3

2

dito Aadhira

The app could also crash at

NSMakeRange(1, [locstr length])

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.

snoopysalive
  • 68
  • 1
  • 5
1
resultString =[resultString substringWithRange:NSMakeRange(1,27)];

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.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
1

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.

Marcelo Alves
  • 1,856
  • 11
  • 12