当我在UITextView中粘贴一些复制的文本时,我的应用程序崩溃了。此崩溃仅发生在iOS15 上
0 CoreFoundation 0x00007fff203feba4 __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007fff201a1be7 objc_exception_throw + 48
2 Foundation 0x00007fff20753aa2 _userInfoForFileAndLine + 0
3 UIKitCore 0x00007fff254164a5 -[_UITextKitTextPosition compare:] + 235
4 UIKitCore 0x00007fff254097b4 -[UITextInputController comparePosition:toPosition:] + 85
5 UIKitCore 0x00007fff2542ac7d -[UITextView comparePosition:toPosition:] + 85
6 UIKitCore 0x00007fff253d0251 -[UITextPasteController _clampRange:] + 634
7 UIKitCore 0x00007fff253d09c4 __87-[UITextPasteController _performPasteOfAttributedString:toRange:forSession:completion:]_block_invoke + 40
8 UIKitCore 0x00007fff253d0bcc __87-[UITextPasteController _performPasteOfAttributedString:toRange:forSession:completion:]_block_invoke.174 + 184
9 UIKitCore 0x00007fff25412629 -[UITextInputController _pasteAttributedString:toRange:completion:] + 471
10 UIKitCore 0x00007fff253d092a -[UITextPasteController _performPasteOfAttributedString:toRange:forSession:completion:] + 726
11 UIKitCore 0x00007fff253cfc07 __49-[UITextPasteController _executePasteForSession:]_block_invoke + 299
12 libdispatch.dylib 0x00007fff2011265a _dispatch_call_block_and_release + 12
13 libdispatch.dylib 0x00007fff2011383a _dispatch_client_callout + 8
14 libdispatch.dylib 0x00007fff20120c88 _dispatch_main_queue_callback_4CF + 1075
15 CoreFoundation 0x00007fff2036c84d __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
16 CoreFoundation 0x00007fff203670aa __CFRunLoopRun + 2772
17 CoreFoundation 0x00007fff203660f3 CFRunLoopRunSpecific + 567
18 GraphicsServices 0x00007fff2c995cd3 GSEventRunModal + 139
19 UIKitCore 0x00007fff25059f42 -[UIApplication _run] + 928
20 UIKitCore 0x00007fff2505eb5e UIApplicationMain + 101
使视图控制器符合UITextPasteDelegate
:
class viewController: UIViewController, UITextPasteDelegate { ...
并设置通过延迟:
textView.pasteDelegate = self
然后实现textPasteConfigurationSupporting
功能:
func textPasteConfigurationSupporting(
_ textPasteConfigurationSupporting: UITextPasteConfigurationSupporting,
performPasteOf attributedString: NSAttributedString,
to textRange: UITextRange)
-> UITextRange {
<#code#>
}
我找到了一个可行的解决方案。如果在表视图中使用UITextView,并且还为包含UITextView的单元格实现了prepareForReuse。请确保在prepareForReuse中将UITextView.text值设置为nil。在我的情况下,我后来这样做了(不是在为重用做准备(,这不知何故导致了一些关于";pos";粘贴时导致崩溃。