Recently I got a few crashes on UITextField
:
Fatal Exception: NSInvalidArgumentException -[<_UIObscurableTextContentStorage: 0x30016cd20> offsetFromLocation:toLocation:] received invalid locations 0 (null)
But I'm not able to reproduce this crash on my side. Here's my code snippet:
dispatch_async(dispatch_get_main_queue(), ^{
UITextPosition *newPosition = textField.endOfDocument;
textField.selectedTextRange = [textField textRangeFromPosition:newPosition toPosition:newPosition]; // crash at this line
});
I'm guessing either the textField.endOfDocument
or UITextRange
variable returned from textRangeFromPosition:toPosition:
could be nil, but I have tried hardcoding these variables to nil and still cannot reproduce this crash. Besides, Apple Document claims that textField.endOfDocument
is not an optional value.
Therefore, does anyone have any clue about the root cause for this crash?