iOS15 UITextView 拖动视图后导致崩溃,Invalid参数不满足:pos

Jer*_*sey 6 uitextview uitextviewdelegate ios ios15

崩溃示例

上面的文章是论坛上一位网友发现的问题。最近,我也在firebase上发现了类似的崩溃堆栈。我猜也是这个原因造成的,可能是数据越界的问题。你有好的解决办法吗?

Firebase 使堆栈崩溃:

Fatal Exception: NSInternalInconsistencyException
Invalid parameter not satisfying: pos
0
CoreFoundation
__exceptionPreprocess
1
libobjc.A.dylib
objc_exception_throw
2
Foundation
_userInfoForFileAndLine
3
UIKitCore
-[_UITextKitTextPosition compare:]
4
UIKitCore
-[UITextInputController comparePosition:toPosition:]
5
UIKitCore
-[UITextView comparePosition:toPosition:]
6
UIKitCore
-[UITextPasteController _clampRange:]
7
UIKitCore
__87-[UITextPasteController _performPasteOfAttributedString:toRange:forSession:completion:]_block_invoke
8
UIKitCore
__87-[UITextPasteController _performPasteOfAttributedString:toRange:forSession:completion:]_block_invoke.177
9
UIKitCore
-[UITextInputController _pasteAttributedString:toRange:completion:]
10
UIKitCore
-[UITextPasteController _performPasteOfAttributedString:toRange:forSession:completion:]
11
UIKitCore
__49-[UITextPasteController _executePasteForSession:]_block_invoke
12
libdispatch.dylib
_dispatch_call_block_and_release
13
libdispatch.dylib
_dispatch_client_callout
14
libdispatch.dylib
_dispatch_main_queue_callback_4CF
15
CoreFoundation
__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
16
CoreFoundation
__CFRunLoopRun
17
CoreFoundation
CFRunLoopRunSpecific
18
GraphicsServices
GSEventRunModal
19
UIKitCore
-[UIApplication _run]
20
UIKitCore
UIApplicationMain
21
HelloTalk_Binary
main.m -  20 
main + 20
Run Code Online (Sandbox Code Playgroud)

小智 7

我找不到原因,但找到了解决办法。UITextPasteDelegate如果我实现forUITextViewtextPasteConfigurationSupporting的两种方法之一performPasteOf来返回不带任何属性的字符串,则应用程序不会崩溃。

public func textPasteConfigurationSupporting(
  _ textPasteConfigurationSupporting: UITextPasteConfigurationSupporting,
  performPasteOf attributedString: NSAttributedString,
  to textRange: UITextRange
) -> UITextRange {
  let start = textView.offset(from: textView.beginningOfDocument, to: textRange.start)
  let length = textView.offset(from: textRange.start, to: textRange.end)
  let nsRange = NSRange(location: start, length: length)

  let shouldInsert = textView(
   textView,
   shouldChangeTextIn: nsRange,
   replacementText: attributedString.string
  )

  if shouldInsert {
   textView.replace(textRange, withText: attributedString.string)
  }

  return textRange
}
Run Code Online (Sandbox Code Playgroud)

或者

public func textPasteConfigurationSupporting(
  _ textPasteConfigurationSupporting: UITextPasteConfigurationSupporting,
  combineItemAttributedStrings itemStrings: [NSAttributedString],
  for textRange: UITextRange
) -> NSAttributedString {
  return NSAttributedString(string: itemStrings.map { $0.string }.joined())
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!实现 func textPasteConfigurationSupporting( _ textPasteConfigurationSupporting: UITextPasteConfigurationSupporting, PerformPasteOf attributeString: NSAttributedString, to textRange: UITextRange ) -> UITextRange 对我有用!(另一个没有)不要忘记 textView.pasteDelegate = self (3认同)