由于未捕获的异常 'NSInternalInconsistencyException' 而终止应用程序,原因:'Invalid parameter not satisfying: pos'

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: pos'

当我在 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#>
}

我找到了适合我的可行解决方案。如果您在 table 视图中使用 UITextView,并且还为包含 UITextView 的单元格实施了 prepareForReuse。确保在 prepareForReuse 中将 UITextView.text 值设置为 nil。在我的例子中,我后来做了那件事(不是在 prepareForReuse 中),这在粘贴导致崩溃时以某种方式导致了关于“pos”的一些不一致问题。