崩溃示例
以上文章是一位网友在论坛上发现的问题。最近,我还在firebase上发现了一个类似的崩溃堆栈。我猜也是这个原因造成的,可能是数据越界的问题。你有好的解决办法吗?
Firebase crash the stack:
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
我找不到原因,但找到了工作。如果我为UITextView
,textPasteConfigurationSupporting
或performPasteOf
实现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作为答案时才会发生崩溃
补充道:
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
BOOL success = NO;
NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
if([self isNumericString:newString] || [newString isEqualToString:@""])
{
success = [newString floatValue] <= maxValue
}
else
{
if ([text isEqualToString:@"n"])
{
[textView resignFirstResponder];
}
}
return success;
}