簡體   English   中英

iOS15 UITextView 拖動視圖后,導致崩潰,無效參數不滿足:pos

[英]iOS15 UITextView After dragging the view, it causes a crash, Invalid parameter not satisfying: pos

崩潰示例

以上文章是網友在論壇上發現的一個問題。 最近,我也在 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

我找不到原因,但找到了解決方法。 如果我為UITextViewtextPasteConfigurationSupportingperformPasteOf實現UITextPasteDelegate的兩種方法之一,以返回沒有任何屬性的字符串,則應用程序不會崩潰。

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
}

或者

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

@Sibcat 有一個很好的收獲。

方法textPasteConfigurationSupporting:performPasteOfAttributedString:toRange:的文檔說,如果你實現這個方法 - 沒有應用系統機制(發生崩潰的地方)

如果沒有實現,將使用標准粘貼機制。 所以只要實現這個方法,應用程序就可以避免崩潰。

附加說明

僅當您從委托方法textView:shouldChangeTextInRange:replacementText:text返回 NO 作為答案時才會發生崩潰

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM