滚动浏览UITableView仅在iPhone 5C上导致异常和崩溃 [英] Scrolling through UITableView causes Exception and Crash only on iPhone 5C

查看:247
本文介绍了滚动浏览UITableView仅在iPhone 5C上导致异常和崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户滚动我的 UITableView 太快或者点击状态栏跳转到表格顶部时,会抛出异常。我只是在iPhone 5C设备上看到这次崩溃。我无法在4S,5或32位模拟器上重现它。



使用All Exceptions断点,我能够打印出所有回溯我可以通过崩溃递增帧指针。因为在回溯中没有我的应用程序的痕迹(除了主要),我认为这是一个Apple bug,与 [NSRLEArray objectAtIndex:effectiveRange:] 有关。 这个问题的作者似乎与操作系统有同样的问题X应用程序,以及建议查看我的应用程序中的 NSAttributedString 用法。我在表格视图单元格中使用 NSAttributedStrings 来超链接 UITextView 中的某些字符串。但如果这是 NSAttributedString 的一般问题,这不会导致其他iOS设备崩溃吗?



Backtrace:

 (lldb)bt 
* thread#1:tid = 0x2e57bd,0x3978e6a0 libobjc.A.dylib `objc_exception_throw,queue ='com.apple.main-thread',stop reason = breakpoint 1.1
frame#0:0x3978e6a0 libobjc.A.dylib`objc_exception_throw
frame#1:0x2f431dc4 CoreFoundation` + [NSException raise:format:] + 112
frame#2:0x2fd55230 Foundation`- [NSRLEArray objectAtIndex:effectiveRange:] + 120
frame#3:0x2fd71d76 Foundation`- [NSConcreteMutableAttributedString addAttribute:value:range:] + 198
帧#4:0x36d7b278 UIFoundation`__50- [NSConcreteTextStorage addAttribute:value:range:] _ block_invoke + 156
frame#5:0x36d7b102 UIFoundation`- [NSConcreteTextStorage addAttribute:value:range:] + 170
帧#6:0x337fc02e DataDetectorsUI`- [DDTextKitOperation _addResultsToAttributes] + 2 66
帧#7:0x337fc1d6 DataDetectorsUI`__46- [DDTextKitOperation doURLificationOnDocument] _block_invoke_2 + 26
frame#8:0x36d79a5e UIFoundation`- [NSTextStorage(ActorSupport)coordinateAccess:] + 34
frame#9 :0x337fc1b8 DataDetectorsUI`__46- [DDTextKitOperation doURLificationOnDocument] _block_invoke + 116
frame#10:0x39c78bde libdispatch.dylib`_dispatch_barrier_sync_f_slow_invoke + 66
frame#11:0x39c730ae libdispatch.dylib`_dispatch_client_callout + 22
frame #12:0x39c759a8 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 268
帧#13:0x2f3fc5b0 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
帧#14:0x2f3fae7c CoreFoundation`__CFRunLoopRun + 1308
帧#15:0x2f365470 CoreFoundation` CFRunLoopRunSpecific + 524
帧#16:0x2f365252 CoreFoundation`CFRunLoopRunInMode + 106
帧#17:0x3409f2ea GraphicsServices`GSEventRunModal + 138
帧#18:0x31c 1a844 UIKit`UIApplicationMain + 1136
* frame#19:0x000bffb4 [AppName]`main(argc = 1,argv = 0x27d4ecec)+ 116 at main.m:16
(lldb)bt
* thread#1:tid = 0x2e57bd,0x39153994 libc ++ abi.dylib`__cxa_throw,queue ='com.apple.main-thread',stop reason = breakpoint 1.2
frame#0:0x39153994 libc ++ abi。 dylib`__cxa_throw
frame#1:0x3978e79a libobjc.A.dylib`objc_exception_throw + 250
frame#2:0x2f431dc4 CoreFoundation` + [NSException raise:format:] + 112
frame#3: 0x2fd55230 Foundation`- [NSRLEArray objectAtIndex:effectiveRange:] + 120
frame#4:0x2fd71d76 Foundation`- [NSConcreteMutableAttributedString addAttribute:value:range:] + 198
frame#5:0x36d7b278 UIFoundation`__50- [ NSConcreteTextStorage addAttribute:value:range:] _ block_invoke + 156
frame#6:0x36d7b102 UIFoundation`- [NSConcreteTextStorage addAttribute:value:range:] + 170
frame#7:0x337fc02e DataDetectorsUI`- [DDTextKitOperation _addResultsToAttribute s] + 266
帧#8:0x337fc1d6 DataDetectorsUI`__46- [DDTextKitOperation doURLificationOnDocument] _block_invoke_2 + 26
frame#9:0x36d79a5e UIFoundation`- [NSTextStorage(ActorSupport)coordinateAccess:] + 34
帧#10:0x337fc1b8 DataDetectorsUI`__46- [DDTextKitOperation doURLificationOnDocument] _block_invoke + 116
frame#11:0x39c78bde libdispatch.dylib`_dispatch_barrier_sync_f_slow_invoke + 66
frame#12:0x39c730ae libdispatch.dylib`_dispatch_client_callout + 22
帧#13:0x39c759a8 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 268
帧#14:0x2f3fc5b0 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
帧#15:0x2f3fae7c CoreFoundation`__CFRunLoopRun + 1308
帧#16: 0x2f365470 CoreFoundation`CFRunLoopRunSpecific + 524
frame#17:0x2f365252 CoreFoundation`CFRunLoopRunInMode + 106
frame#18:0x3409f2ea GraphicsServices`GSEventRunModal + 138
frame#19 :0x31c1a844 UIKit`UIApplicationMain + 1136
*帧#20:0x000bffb4 [AppName]`main(argc = 1,argv = 0x27d4ecec)+ 116 at main.m:16
(lldb)bt
*线程#1:tid = 0x2e57bd,0x3978e6a0 libobjc.A.dylib`objc_exception_throw,queue ='com.apple.main-thread',stop reason = breakpoint 1.1
frame#0:0x3978e6a0 libobjc.A.dylib `objc_exception_throw
帧#1:0x2f431b88 CoreFoundation`- [NSException raise] + 8
帧#2:0x36d7b19a UIFoundation`- [NSConcreteTextStorage addAttribute:value:range:] + 322
frame#3 :0x337fc02e DataDetectorsUI`- [DDTextKitOperation _addResultsToAttributes] + 266
frame#4:0x337fc1d6 DataDetectorsUI`__46- [DDTextKitOperation doURLificationOnDocument] _block_invoke_2 + 26
frame#5:0x36d79a5e UIFoundation`- [NSTextStorage(ActorSupport)coordinateAccess: ] + 34
帧#6:0x337fc1b8 DataDetectorsUI`__46- [DDTextKitOperation doURLificationOnDocument] _block_invoke + 116
frame#7:0x39c78bde libdisp atch.dylib`_dispatch_barrier_sync_f_slow_invoke + 66
frame#8:0x39c730ae libdispatch.dylib`_dispatch_client_callout + 22
frame#9:0x39c759a8 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 268
frame#10:0x2f3fc5b0 CoreFoundation` __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
帧#11:0x2f3fae7c CoreFoundation`__CFRunLoopRun + 1308
帧#12:0x2f365470 CoreFoundation`CFRunLoopRunSpecific + 524
帧#13:0x2f365252 CoreFoundation`CFRunLoopRunInMode + 106
帧#14:0x3409f2ea GraphicsServices`GSEventRunModal + 138
frame#15:0x31c1a844 UIKit`UIApplicationMain + 1136
* frame#16:0x000bffb4 [AppName]`main(argc = 1,argv = 0x27d4ecec)+ 116 at main.m:16
(lldb)bt
* thread#1:tid = 0x2e57bd,0x39153994 libc ++ abi.dylib`__cxa_throw,queue ='com.apple.main-thread',停止原因=断点1.2
帧#0:0x39153994 libc ++ abi.dylib`__cxa_throw
帧#1:0x3978e79a lib objc.A.dylib`objc_exception_throw + 250
帧#2:0x2f431b88 CoreFoundation`- [NSException raise] + 8
frame#3:0x36d7b19a UIFoundation`- [NSConcreteTextStorage addAttribute:value:range:] + 322
frame#4:0x337fc02e DataDetectorsUI`- [DDTextKitOperation _addResultsToAttributes] + 266
frame#5:0x337fc1d6 DataDetectorsUI`__46- [DDTextKitOperation doURLificationOnDocument] _block_invoke_2 + 26
frame#6:0x36d79a5e UIFoundation`- [NSTextStorage(ActorSupport)coordinateAccess:] + 34
frame#7:0x337fc1b8 DataDetectorsUI`__46- [DDTextKitOperation doURLificationOnDocument] _block_invoke + 116
frame#8:0x39c78bde libdispatch.dylib`_dispatch_barrier_sync_f_slow_invoke + 66
帧#9:0x39c730ae libdispatch.dylib`_dispatch_client_callout + 22
帧#10:0x39c759a8 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 268
帧#11:0x2f3fc5b0 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
帧#12:0x2f3fae7c CoreFoundation`__CFRunLoopRun + 1308
帧#13:0x2f365470 CoreFoundation`CFRunLoopRunSpecific + 524
帧#14:0x2f365252 CoreFoundation`CFRunLoopRunInMode + 106
帧#15:0x3409f2ea GraphicsServices`GSEventRunModal + 138
帧#16:0x31c1a844 UIKit`UIApplicationMain + 1136
*帧#17:0x000bffb4 [AppName]`main(argc = 1,argv = 0x27d4ecec)+ 116 at main.m:16
(lldb)bt
* thread#1:tid = 0x2e57bd,0x39d3e1fc libsystem_kernel.dylib`__pthread_kill + 8,queue ='com.apple.main-thread',stop reason = signal SIGABRT
frame #0:0x39d3e1fc libsystem_kernel.dylib`__pthread_kill + 8
frame#1:0x39da5a52 libsystem_pthread.dylib`pthread_kill + 58
frame#2:0x39cef02c libsystem_c.dylib`abort + 76
frame#3 :0x001f32ba [AppName]`uncaught_exception_handler + 26
frame#4:0x2f432184 CoreFoundation`__handleUncaughtException + 580
frame#5:0x3978e926 libobjc.A.dylib`_ob jc_terminate()+ 174
帧#6:0x391541b2 libc ++ abi.dylib`std :: __ terminate(void(*)())+ 78
frame#7:0x39153d16 libc ++ abi.dylib `__cxa_rethrow + 102
帧#8:0x3978e80e libobjc.A.dylib`objc_exception_rethrow + 42
帧#9:0x36d7b150 UIFoundation`- [NSConcreteTextStorage addAttribute:value:range:] + 248
frame #10:0x337fc02e DataDetectorsUI`- [DDTextKitOperation _addResultsToAttributes] + 266
frame#11:0x337fc1d6 DataDetectorsUI`__46- [DDTextKitOperation doURLificationOnDocument] _block_invoke_2 + 26
frame#12:0x36d79a5e UIFoundation`- [NSTextStorage(ActorSupport) coordinateAccess:] + 34
frame#13:0x337fc1b8 DataDetectorsUI`__46- [DDTextKitOperation doURLificationOnDocument] _block_invoke + 116
frame#14:0x39c78bde libdispatch.dylib`_dispatch_barrier_sync_f_slow_invoke + 66
frame#15:0x39c730ae libdispatch.dylib`_dispatch_client_callout + 22
frame#16:0x39c759a8 libdispatch.dylib`_dispatc h_main_queue_callback_4CF + 268
帧#17:0x2f3fc5b0 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
帧#18:0x2f3fae7c CoreFoundation`__CFRunLoopRun + 1308
帧#19:0x2f365470 CoreFoundation`CFRunLoopRunSpecific + 524
帧#20:0x2f365252 CoreFoundation`CFRunLoopRunInMode + 106
帧#21:0x3409f2ea GraphicsServices`GSEventRunModal + 138
帧#22:0x31c1a844 UIKit`UIApplicationMain + 1136
*帧#23:0x000bffb4 [AppName] `main(argc = 1,argv = 0x27d4ecec)+ 116 at main.m:16
(lldb)bt
* thread#22:tid = 0x2e57ea,0x39da2cbc libsystem_pthread.dylib`start_wqthread,stop reason = signal SIGABRT
* frame#0:0x39da2cbc libsystem_pthread.dylib`start_wqthread

这是我的地方在代码
tweetTextView.attributedText = [[NSAttributedString alloc] initWithString: tweetTextView.text];
...

- (void)findHashtagsAndAtMentionsInTweetText:(UITextView *)textView {

NSString * tweetText = textView.text;

NSError * error = nil;
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:@(@(\\\\ +))|(^ | \\ W)(#| \\\\\\\ \\\\ {L} \\ w *)选项:0错误:&错误];
NSArray * matches = [regex matchesInString:tweetText options:0 range:NSMakeRange(0,tweetText.length)];

NSMutableAttributedString * myTagOrMention = [[NSMutableAttributedString alloc] initWithString:tweetTextView.text];

for(NSTextCheckingResult *匹配中匹配){

NSRange wordRange = [match rangeAtIndex:0]; //导致崩溃?
NSString * word = [tweetText substringWithRange:wordRange];

if([word rangeOfString:@@]。location!= NSNotFound){

// @提及

//删除空格和@
NSString * formattedTag = [NSString stringWithFormat:@%@,[[word stringByReplacingOccurrencesOfString:@withString:@] stringByReplacingOccurrencesOfString:@@withString:@]];

NSString * tweetContets = tweetTextView.text;
NSRange range = [tweetContets rangeOfString:word];

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@twitter://]]){
NSString * twitterAppURL = [NSString stringWithFormat:@%@%@ ,@twitter:// user?screen_name =,formattedTag];
[myTagOrMention addAttribute:NSLinkAttributeName value:twitterAppURL range:range];
} else {

[myTagOrMention addAttribute:NSLinkAttributeName value:[NSString stringWithFormat:@%@%@,@http://mobile.twitter.com/,formattedTag]范围:范围];
}

tweetTextView.attributedText = myTagOrMention;

} else {
// hashtag

//删除whitespaces和pound
NSString * formattedTag = [NSString stringWithFormat:@%@, [[word stringByReplacingOccurrencesOfString:@withString:@] stringByReplacingOccurrencesOfString:@#withString:@]];

NSString * tweetContets = tweetTextView.text;
NSRange range = [tweetContets rangeOfString:word];

[myTagOrMention addAttribute:NSLinkAttributeName value:[NSString stringWithFormat:@%@%@%@%@,@http://twitter.com/search/,@?q = %23,formattedTag,@& s = hash] range:range];

tweetTextView.attributedText = myTagOrMention;
}
}


解决方案

错误不在您发布的代码中。当一个 UITextView 在其链接仍然生成时被重用时会发生错误,这是异步完成的。



查看这些类似的Stack Overflow问题:





许多用户报告将文本视图的文本属性设置为<$ c单元格的 prepareForReuse 方法中的$ c> nil 解决了该问题。或者,您可以使用像 TTTAttributedLabel 这样的库而不是文本视图。



它只出现在iPhone 5C可能是红鲱鱼。我打赌这个设备仍在运行iOS 7,其他设备已经更新到更新版本的iOS。






在旁注中,可能存在正则表达式未涵盖的边缘情况。我会删除你的twitter解析方法,并将其替换为官方 twitter-text -objc


There is an exception being thrown when a user scrolls through my UITableView too fast or taps the status bar to jump to the top of the table. I am only seeing this crash on an iPhone 5C device. I can't reproduce it on the 4S, 5, or with the 32-bit simulator.

Using the "All Exceptions" breakpoint, I was able to print out all the backtraces I could incrementing the frame pointer through the crash. Because there is no trace of my app in the backtrace (besides the main), I assume this is an Apple bug, something to do with [NSRLEArray objectAtIndex:effectiveRange:]. The author of this question seemed to have the same problem with an OS X application, and a comment suggested looking at the NSAttributedString usage in my app. I do use NSAttributedStrings in my table view cells to hyperlink certain strings in a UITextView. But if it was a general issue with NSAttributedString, wouldn't this also cause a crash on other iOS devices?

Backtrace:

(lldb) bt
* thread #1: tid = 0x2e57bd, 0x3978e6a0 libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x3978e6a0 libobjc.A.dylib`objc_exception_throw
    frame #1: 0x2f431dc4 CoreFoundation`+[NSException raise:format:] + 112
    frame #2: 0x2fd55230 Foundation`-[NSRLEArray objectAtIndex:effectiveRange:] + 120
    frame #3: 0x2fd71d76 Foundation`-[NSConcreteMutableAttributedString addAttribute:value:range:] + 198
    frame #4: 0x36d7b278 UIFoundation`__50-[NSConcreteTextStorage addAttribute:value:range:]_block_invoke + 156
    frame #5: 0x36d7b102 UIFoundation`-[NSConcreteTextStorage addAttribute:value:range:] + 170
    frame #6: 0x337fc02e DataDetectorsUI`-[DDTextKitOperation _addResultsToAttributes] + 266
    frame #7: 0x337fc1d6 DataDetectorsUI`__46-[DDTextKitOperation doURLificationOnDocument]_block_invoke_2 + 26
    frame #8: 0x36d79a5e UIFoundation`-[NSTextStorage(ActorSupport) coordinateAccess:] + 34
    frame #9: 0x337fc1b8 DataDetectorsUI`__46-[DDTextKitOperation doURLificationOnDocument]_block_invoke + 116
    frame #10: 0x39c78bde libdispatch.dylib`_dispatch_barrier_sync_f_slow_invoke + 66
    frame #11: 0x39c730ae libdispatch.dylib`_dispatch_client_callout + 22
    frame #12: 0x39c759a8 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 268
    frame #13: 0x2f3fc5b0 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
    frame #14: 0x2f3fae7c CoreFoundation`__CFRunLoopRun + 1308
    frame #15: 0x2f365470 CoreFoundation`CFRunLoopRunSpecific + 524
    frame #16: 0x2f365252 CoreFoundation`CFRunLoopRunInMode + 106
    frame #17: 0x3409f2ea GraphicsServices`GSEventRunModal + 138
    frame #18: 0x31c1a844 UIKit`UIApplicationMain + 1136
  * frame #19: 0x000bffb4 [AppName]`main(argc=1, argv=0x27d4ecec) + 116 at main.m:16
(lldb) bt
* thread #1: tid = 0x2e57bd, 0x39153994 libc++abi.dylib`__cxa_throw, queue = 'com.apple.main-thread', stop reason = breakpoint 1.2
    frame #0: 0x39153994 libc++abi.dylib`__cxa_throw
    frame #1: 0x3978e79a libobjc.A.dylib`objc_exception_throw + 250
    frame #2: 0x2f431dc4 CoreFoundation`+[NSException raise:format:] + 112
    frame #3: 0x2fd55230 Foundation`-[NSRLEArray objectAtIndex:effectiveRange:] + 120
    frame #4: 0x2fd71d76 Foundation`-[NSConcreteMutableAttributedString addAttribute:value:range:] + 198
    frame #5: 0x36d7b278 UIFoundation`__50-[NSConcreteTextStorage addAttribute:value:range:]_block_invoke + 156
    frame #6: 0x36d7b102 UIFoundation`-[NSConcreteTextStorage addAttribute:value:range:] + 170
    frame #7: 0x337fc02e DataDetectorsUI`-[DDTextKitOperation _addResultsToAttributes] + 266
    frame #8: 0x337fc1d6 DataDetectorsUI`__46-[DDTextKitOperation doURLificationOnDocument]_block_invoke_2 + 26
    frame #9: 0x36d79a5e UIFoundation`-[NSTextStorage(ActorSupport) coordinateAccess:] + 34
    frame #10: 0x337fc1b8 DataDetectorsUI`__46-[DDTextKitOperation doURLificationOnDocument]_block_invoke + 116
    frame #11: 0x39c78bde libdispatch.dylib`_dispatch_barrier_sync_f_slow_invoke + 66
    frame #12: 0x39c730ae libdispatch.dylib`_dispatch_client_callout + 22
    frame #13: 0x39c759a8 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 268
    frame #14: 0x2f3fc5b0 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
    frame #15: 0x2f3fae7c CoreFoundation`__CFRunLoopRun + 1308
    frame #16: 0x2f365470 CoreFoundation`CFRunLoopRunSpecific + 524
    frame #17: 0x2f365252 CoreFoundation`CFRunLoopRunInMode + 106
    frame #18: 0x3409f2ea GraphicsServices`GSEventRunModal + 138
    frame #19: 0x31c1a844 UIKit`UIApplicationMain + 1136
  * frame #20: 0x000bffb4 [AppName]`main(argc=1, argv=0x27d4ecec) + 116 at main.m:16
(lldb) bt
* thread #1: tid = 0x2e57bd, 0x3978e6a0 libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x3978e6a0 libobjc.A.dylib`objc_exception_throw
    frame #1: 0x2f431b88 CoreFoundation`-[NSException raise] + 8
    frame #2: 0x36d7b19a UIFoundation`-[NSConcreteTextStorage addAttribute:value:range:] + 322
    frame #3: 0x337fc02e DataDetectorsUI`-[DDTextKitOperation _addResultsToAttributes] + 266
    frame #4: 0x337fc1d6 DataDetectorsUI`__46-[DDTextKitOperation doURLificationOnDocument]_block_invoke_2 + 26
    frame #5: 0x36d79a5e UIFoundation`-[NSTextStorage(ActorSupport) coordinateAccess:] + 34
    frame #6: 0x337fc1b8 DataDetectorsUI`__46-[DDTextKitOperation doURLificationOnDocument]_block_invoke + 116
    frame #7: 0x39c78bde libdispatch.dylib`_dispatch_barrier_sync_f_slow_invoke + 66
    frame #8: 0x39c730ae libdispatch.dylib`_dispatch_client_callout + 22
    frame #9: 0x39c759a8 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 268
    frame #10: 0x2f3fc5b0 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
    frame #11: 0x2f3fae7c CoreFoundation`__CFRunLoopRun + 1308
    frame #12: 0x2f365470 CoreFoundation`CFRunLoopRunSpecific + 524
    frame #13: 0x2f365252 CoreFoundation`CFRunLoopRunInMode + 106
    frame #14: 0x3409f2ea GraphicsServices`GSEventRunModal + 138
    frame #15: 0x31c1a844 UIKit`UIApplicationMain + 1136
  * frame #16: 0x000bffb4 [AppName]`main(argc=1, argv=0x27d4ecec) + 116 at main.m:16
(lldb) bt
* thread #1: tid = 0x2e57bd, 0x39153994 libc++abi.dylib`__cxa_throw, queue = 'com.apple.main-thread', stop reason = breakpoint 1.2
    frame #0: 0x39153994 libc++abi.dylib`__cxa_throw
    frame #1: 0x3978e79a libobjc.A.dylib`objc_exception_throw + 250
    frame #2: 0x2f431b88 CoreFoundation`-[NSException raise] + 8
    frame #3: 0x36d7b19a UIFoundation`-[NSConcreteTextStorage addAttribute:value:range:] + 322
    frame #4: 0x337fc02e DataDetectorsUI`-[DDTextKitOperation _addResultsToAttributes] + 266
    frame #5: 0x337fc1d6 DataDetectorsUI`__46-[DDTextKitOperation doURLificationOnDocument]_block_invoke_2 + 26
    frame #6: 0x36d79a5e UIFoundation`-[NSTextStorage(ActorSupport) coordinateAccess:] + 34
    frame #7: 0x337fc1b8 DataDetectorsUI`__46-[DDTextKitOperation doURLificationOnDocument]_block_invoke + 116
    frame #8: 0x39c78bde libdispatch.dylib`_dispatch_barrier_sync_f_slow_invoke + 66
    frame #9: 0x39c730ae libdispatch.dylib`_dispatch_client_callout + 22
    frame #10: 0x39c759a8 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 268
    frame #11: 0x2f3fc5b0 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
    frame #12: 0x2f3fae7c CoreFoundation`__CFRunLoopRun + 1308
    frame #13: 0x2f365470 CoreFoundation`CFRunLoopRunSpecific + 524
    frame #14: 0x2f365252 CoreFoundation`CFRunLoopRunInMode + 106
    frame #15: 0x3409f2ea GraphicsServices`GSEventRunModal + 138
    frame #16: 0x31c1a844 UIKit`UIApplicationMain + 1136
  * frame #17: 0x000bffb4 [AppName]`main(argc=1, argv=0x27d4ecec) + 116 at main.m:16
(lldb) bt
* thread #1: tid = 0x2e57bd, 0x39d3e1fc libsystem_kernel.dylib`__pthread_kill + 8, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x39d3e1fc libsystem_kernel.dylib`__pthread_kill + 8
    frame #1: 0x39da5a52 libsystem_pthread.dylib`pthread_kill + 58
    frame #2: 0x39cef02c libsystem_c.dylib`abort + 76
    frame #3: 0x001f32ba [AppName]`uncaught_exception_handler + 26
    frame #4: 0x2f432184 CoreFoundation`__handleUncaughtException + 580
    frame #5: 0x3978e926 libobjc.A.dylib`_objc_terminate() + 174
    frame #6: 0x391541b2 libc++abi.dylib`std::__terminate(void (*)()) + 78
    frame #7: 0x39153d16 libc++abi.dylib`__cxa_rethrow + 102
    frame #8: 0x3978e80e libobjc.A.dylib`objc_exception_rethrow + 42
    frame #9: 0x36d7b150 UIFoundation`-[NSConcreteTextStorage addAttribute:value:range:] + 248
    frame #10: 0x337fc02e DataDetectorsUI`-[DDTextKitOperation _addResultsToAttributes] + 266
    frame #11: 0x337fc1d6 DataDetectorsUI`__46-[DDTextKitOperation doURLificationOnDocument]_block_invoke_2 + 26
    frame #12: 0x36d79a5e UIFoundation`-[NSTextStorage(ActorSupport) coordinateAccess:] + 34
    frame #13: 0x337fc1b8 DataDetectorsUI`__46-[DDTextKitOperation doURLificationOnDocument]_block_invoke + 116
    frame #14: 0x39c78bde libdispatch.dylib`_dispatch_barrier_sync_f_slow_invoke + 66
    frame #15: 0x39c730ae libdispatch.dylib`_dispatch_client_callout + 22
    frame #16: 0x39c759a8 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 268
    frame #17: 0x2f3fc5b0 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
    frame #18: 0x2f3fae7c CoreFoundation`__CFRunLoopRun + 1308
    frame #19: 0x2f365470 CoreFoundation`CFRunLoopRunSpecific + 524
    frame #20: 0x2f365252 CoreFoundation`CFRunLoopRunInMode + 106
    frame #21: 0x3409f2ea GraphicsServices`GSEventRunModal + 138
    frame #22: 0x31c1a844 UIKit`UIApplicationMain + 1136
  * frame #23: 0x000bffb4 [AppName]`main(argc=1, argv=0x27d4ecec) + 116 at main.m:16
(lldb) bt
* thread #22: tid = 0x2e57ea, 0x39da2cbc libsystem_pthread.dylib`start_wqthread, stop reason = signal SIGABRT
  * frame #0: 0x39da2cbc libsystem_pthread.dylib`start_wqthread

Here is where I set the attributed string:

// earlier in the code
tweetTextView.attributedText = [[NSAttributedString alloc] initWithString:tweetTextView.text];
...

-(void)findHashtagsAndAtMentionsInTweetText:(UITextView *)textView {

    NSString *tweetText = textView.text;

    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(@(\\w+))|(^|\\W)(#|\\uFF03)(\\w*\\p{L}\\w*)" options:0 error:&error];
    NSArray *matches = [regex matchesInString:tweetText options:0 range:NSMakeRange(0, tweetText.length)];

    NSMutableAttributedString * myTagOrMention = [[NSMutableAttributedString alloc] initWithString:tweetTextView.text];

    for (NSTextCheckingResult *match in matches) {

        NSRange wordRange = [match rangeAtIndex:0]; // causing crash?
        NSString* word = [tweetText substringWithRange:wordRange];

        if([word rangeOfString:@"@"].location != NSNotFound) {

            // @ mention

            // remove whitespaces and @
            NSString *formattedTag = [NSString stringWithFormat:@"%@", [[word stringByReplacingOccurrencesOfString:@" " withString:@""] stringByReplacingOccurrencesOfString:@"@" withString:@""]];

            NSString *tweetContets = tweetTextView.text;
            NSRange range = [tweetContets rangeOfString:word];

            if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"twitter://"]]) {
                NSString *twitterAppURL = [NSString stringWithFormat:@"%@%@", @"twitter://user?screen_name=",formattedTag];
                [myTagOrMention addAttribute:NSLinkAttributeName value:twitterAppURL range:range];
            } else {

                [myTagOrMention addAttribute:NSLinkAttributeName value:[NSString stringWithFormat:@"%@%@", @"http://mobile.twitter.com/",formattedTag] range:range];
            }

            tweetTextView.attributedText = myTagOrMention;

        } else {
            // hashtag

            // remove whitespaces and pound
            NSString *formattedTag = [NSString stringWithFormat:@"%@", [[word stringByReplacingOccurrencesOfString:@" " withString:@""] stringByReplacingOccurrencesOfString:@"#" withString:@""]];

            NSString *tweetContets = tweetTextView.text;
            NSRange range = [tweetContets rangeOfString:word];

            [myTagOrMention addAttribute:NSLinkAttributeName value:[NSString stringWithFormat:@"%@%@%@%@", @"http://twitter.com/search/", @"?q=%23", formattedTag, @"&s=hash"] range:range];

            tweetTextView.attributedText = myTagOrMention;
        }
    }

解决方案

The bug is not in the code you posted. The bug happens when a UITextView is reused while its links are still being generated, which is done asynchronously.

See these similar Stack Overflow questions:

Many users have reported that setting the text view's text property to nil in the cell's prepareForReuse method resolves the issue. Alternatively you could use a library like TTTAttributedLabel instead of a text view.

The fact that it only appears on an iPhone 5C is probably a red herring. I bet this device is still running iOS 7, and the other devices have been updated to a newer version of iOS.


On a side note, there may be edge cases that your regex doesn't cover. I would delete your twitter parsing method and replace it with the official twitter-text-objc library.

这篇关于滚动浏览UITableView仅在iPhone 5C上导致异常和崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆