使用NSString方法进行神秘崩溃rangeOfComposedCharacterSequenceAtIndex:

Tim*_*ber 9 cocoa-touch objective-c nsstring ios

我的应用程序使用了几个UITextView,我们正在收到用户无法重现的崩溃报告.

崩溃报告(似乎)不包含任何代码,并且NSInvalidArgumentExceptionNSString方法中崩溃rangeOfComposedCharacterSequenceAtIndex:,而我们的代码不会直接调用它,但似乎是由框架调用.

这是崩溃报告:

0  CoreFoundation    __exceptionPreprocess + 130
1 libobjc.A.dylib    objc_exception_throw + 38
2  CoreFoundation    -[NSException initWithCoder:]
3  Foundation        -[NSString rangeOfComposedCharacterSequenceAtIndex:] + 88
4  UIKit             __74-[UITextInputController _validCaretPositionFromCharacterIndex:downstream:]_block_invoke + 328
5  UIFoundation      -[NSTextStorage coordinateReading:] + 36
6  UIKit             -[UITextInputController _validCaretPositionFromCharacterIndex:downstream:] + 218
7  UIKit             __52-[UITextInputController _characterPositionForPoint:]_block_invoke + 1112
8  UIFoundation      -[NSLayoutManager(TextLocking) coordinateAccess:] + 46
9  UIKit             -[UITextInputController _characterPositionForPoint:] + 224
10 UIKit             -[UITextSelection setSelectionWithFirstPoint:secondPoint:] + 56
11 UIKit             -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) twoFingerRangedSelectGesture:] + 386
12 UIKit             _UIGestureRecognizerSendActions + 196
13 UIKit             -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 1138
14 UIKit             ___UIGestureRecognizerUpdate_block_invoke + 48
15 UIKit             _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 218
16 UIKit             _UIGestureRecognizerUpdate + 282
17 CoreFoundation    __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
18 CoreFoundation    __CFRunLoopDoObservers + 284
19 CoreFoundation    __CFRunLoopRun + 730
20 CoreFoundation    CFRunLoopRunSpecific + 522
21 CoreFoundation    CFRunLoopRunInMode + 106
22 GraphicsServices  GSEventRunModal + 138
23 UIKit             UIApplicationMain + 1136
24 main.m line 16 
25 libdyld.dylib     start + 2
Run Code Online (Sandbox Code Playgroud)

(https://gist.github.com/timarnold/6981caa6a1ee2b98c2fe)

由于我不确切知道代码的哪一部分导致崩溃,我不确定哪些示例代码对发布有帮助.我更感兴趣的是,如果有人看到过这样的事情,或者可能有关于在何处或如何进一步调查此事的建议.

2013-11-21更新

通过执行以下操作,我能够重现此问题:

  • 添加一些文字给我UITextView,包括一个尾随的换行符(\n)
  • 获取我的应用程序以强制UITextView退出第一响应者状态
  • 获取我的应用程序为我分配第一响应者状态UITextView,然后点击将光标插入字符串的末尾(在换行符的位置周围)

之后,应用程序与上述报告崩溃.

我试图在一个空的Xcode项目中创建这个,只有一个股票UITextView而没有别的,并且无法这样做.似乎我的应用程序中UITextView发生了一些事情,因为发生了这种崩溃.很想知道什么,但是这个问题在我的项目中得到了解决(因为我们对追踪换行符不感兴趣,并且可以修剪它们,从而防止崩溃发生).

如果有人可以在示例项目中重现这一点,那么如果这确实是一个错误,那么提交雷达会很棒UITextView.

感谢@ wattson12和@Johan Kool提供的解决方案.

wat*_*n12 4

我今天遇到了同样的问题,我没有旧设备,但很高兴看看这在 < 7.0.3 上是否有所不同,因为它现在非常容易重现并且之前没有被拾取。我无法真正解释原因,但这是我注意到的以及我使用的修复:

我发现在以下情况下点击文本视图会崩溃:

  • 我在文本末尾有一个新行字符,并且
  • 我点击了当前文本之外的某处

我的解决方案是从末尾修剪换行符(我使用 while 循环来避免删除任何前导换行符,但如果这不是问题的话 stringByTrimmingCharactersInSet 就可以了)