Question

I have an application that scans text from a website in order to get the information and put it into my application. So in this case I'm retrieveing the number of notifications that the user has on facebook. Everything works fine except that the application crashes randomly. This is the code I have for searching for the number inside the text:

   NSScanner *theScanner = [NSScanner scannerWithString:facebookTextF];
NSScanner *theScanner2 = [NSScanner scannerWithString:facebookTextFa];


[theScanner scanUpToString:@"Notifications" intoString:&facebookTextFa] ;
[theScanner scanUpToString:@"\n" intoString:&facebookTextF] ;
NSString *NotificationsValue;
if ([facebookTextF isEqualToString:@"Notifications"]) {
    NotificationsValue = @"0";
    NSLog(@"%@", NotificationsValue);

       } else {
    [theScanner2 scanUpToString:@"Notifications" intoString:nil] ;
    [theScanner2 setScanLocation:([theScanner2 scanLocation] + 13)];
    [theScanner2 scanUpToString:@"\n" intoString:&facebookTextFa] ;

               NSLog(@"%@", facebookTextFa);

                    }

This code works well but just randomly crashes. Here is the crash log I get:

iphone[654:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSConcreteScanner setScanLocation:]: Range or index out of bounds'

* First throw call stack: (0x3361b6c3 0x398b397f 0x3361b5e5 0x34d2632f 0x25147 0x34dbf78b 0x335f09ff 0x335f06b1 0x335ef321 0x3356239d 0x33562229 0x39fad31b 0x3402b8f9 0x22267 0x22208) libc++abi.dylib: terminate called throwing an exception

This crash occurs randomly no matter what. For example if I leave it running for a while, after 7 minutes is just randomly crashes. Not sure what is wrong. Thank you for looking. IF you need any more information please let me know.

Was it helpful?

Solution

You are getting that error message because (apparently) the string sometimes doesn't have the word "Notifications", so the theScanner2 sets its scan location to the end of the string. Then, when you try to set the scan location 13 characters ahead, it's past the end of the string, and you get an out of range error.

OTHER TIPS

Trying to setScanLocation in NSScanner which is out of bound means the end of the string and beyond which doesnot exists by setting scan location

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top