iPhone SDK :NSScanner crashing randomly

标签 iphone ios xcode crash nsscanner

我有一个应用程序,可以扫描网站上的文本以获取信息并将其放入我的应用程序中。因此,在本例中,我正在检索用户在 Facebook 上收到的通知数量。除了应用程序随机崩溃之外,一切正常。这是我用于搜索文本中的数字的代码:

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

                    }

这段代码运行良好,但只是随机崩溃。这是我得到的崩溃日志:

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

* 第一个抛出调用堆栈: (0x3361b6c3 0x398b397f 0x3361b5e5 0x34d2632f 0x25147 0x34dbf78b 0x335f09ff 0x335f06b1 0x335ef321 0x3356239d 0x33562229 0x39fad31 b 0x3402b8f9 0x22267 0x22208) libc++abi.dylib:终止调用并抛出异常

无论如何,此崩溃都是随机发生的。例如,如果我让它运行一段时间,7 分钟后就会随机崩溃。不知道出了什么问题。 谢谢您的关注。如果您需要更多信息,请告诉我。

最佳答案

您收到该错误消息是因为(显然)字符串有时没有单词“Notifications”,因此 theScanner2 将其扫描位置设置为字符串的末尾。然后,当您尝试将扫描位置设置为向前 13 个字符时,它会超出字符串的末尾,并且您会收到超出范围的错误。

关于iPhone SDK :NSScanner crashing randomly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12224027/

相关文章:

ios - iOS 9背景位置更新和使用Web服务在服务器上更新

iphone - UIAlertView 按钮到另一个 View Controller

ios - Storyboard仅以 XML 格式显示

iphone - 获取iOS上已安装的Web浏览器应用程序的列表

ios - Xcode 与 uncrustify : how to align function declaration to colons?

ios - Xcode 意外错误(0xC011)在运行程序后和我按下管理器时

iphone - 无法以编程方式更改 MFMailComposer 语言?

iphone - iOS UIImageView vs drawRect 图像拉伸(stretch)

ios - 从 Cocoapods 迁移到 Swift Package Manager 但导入模块失败

ios - XPath 在 Swift 中无法正常工作