IOS swift 模拟器中UItextView中使用系统键盘输入汉字后立马崩溃,但英文没事,从别处复制粘贴的大段中文也没事,但一输入汉字又立马崩溃,跪求高手解救
Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSCFString substringFromIndex:]: Index 2 out of bounds; string length 1'
*** First throw call stack: ( 0 CoreFoundation 0x0000000108864e65 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010a8cadeb objc_exception_throw + 48 2 CoreFoundation 0x0000000108864d9d +[NSException raise:format:] + 205 3 Foundation 0x0000000108cf711d -[NSString substringFromIndex:] + 126 4 艾特便签 0x00000001066c60dd _TFCX13wtqIJFfHfgzph31CreateInformationViewController17textViewDidChangefS0_FCSo10UITextViewT_ + 509 5 艾特便签 0x00000001066c6a4a _TToFCX13wtqIJFfHfgzph31CreateInformationViewController17textViewDidChangefS0_FCSo10UITextViewT_ + 58 6 UIKit 0x0000000109d51847 -[UITextView textInputDidChange:] + 109 7 UIKit 0x0000000109d97cb7 -[UITextInputController _sendDelegateChangeNotificationsForText:selection:] + 138 8 UIKit 0x0000000109d9f751 -[UITextInputController setMarkedText:selectedRange:] + 744 9 UIKit 0x0000000109d505a4 -[UITextView setMarkedText:selectedRange:] + 75 10 UIKit 0x000000010994bc4a -[UIResponder(UITextInput_Internal) _setMarkedText:selectedRange:] + 91 11 UIKit 0x000000010964a2f8 -[UIKeyboardImpl unmarkText:] + 167 12 UIKit 0x000000010964cdf6 __59-[UIKeyboardImpl handleAcceptedCandidate:executionContext:]_block_invoke_2 + 39 13 UIKit 0x0000000109dba724 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 332 14 UIKit 0x0000000109dbaeb5 -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] + 237 15 UIKit 0x0000000109654ba0 -[UIKeyboardImpl acceptCurrentCandidateForInput:] + 176 16 UIKit 0x0000000109a1c854 -[UIKeyboardCandidateBar collectionView:didSelectItemAtIndexPath:] + 298 17 UIKit 0x0000000109bf94a7 -[UICollectionView _selectItemAtIndexPath:animated:scrollPosition:notifyDelegate:] + 701 18 UIKit 0x0000000109c1b049 -[UICollectionView touchesEnded:withEvent:] + 574 19 UIKit 0x00000001095b4ef7 forwardTouchMethod + 349 20 UIKit 0x00000001095b4fc0 -[UIResponder touchesEnded:withEvent:] + 49 21 UIKit 0x00000001095b4ef7 forwardTouchMethod + 349 22 UIKit 0x00000001095b4fc0 -[UIResponder touchesEnded:withEvent:] + 49 23 UIKit 0x0000000109881ede _UIGestureRecognizerUpdate + 10279 24 UIKit 0x0000000109417f8a -[UIWindow _sendGesturesForEvent:] + 1137 25 UIKit 0x00000001094191c0 -[UIWindow sendEvent:] + 849 26 UIKit 0x00000001093c7b66 -[UIApplication sendEvent:] + 263 27 UIKit 0x00000001093a1d97 _UIApplicationHandleEventQueue + 6844 28 CoreFoundation 0x0000000108790a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 29 CoreFoundation 0x000000010878695c __CFRunLoopDoSources0 + 556 30 CoreFoundation 0x0000000108785e13 __CFRunLoopRun + 867 31 CoreFoundation 0x0000000108785828 CFRunLoopRunSpecific + 488 32 GraphicsServices 0x000000010ccb1ad2 GSEventRunModal + 161 33 UIKit 0x00000001093a7610 UIApplicationMain + 171 34 艾特便签 0x00000001066bb5fd main + 109 35 libdyld.dylib 0x000000010b4c592d start + 1 36 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
没重现这个问题,在我这是好的。估计跟编码有关系,检查一下回调里自己写的代码吧。另外如果只是模拟器有问题,真机没事就不用管它了,模拟器跟真机不是完全一样的。
错误提示的很明显,substringFromIndex 操作访问越界了。你可以检查一下程序里面该部分对应的代码。