UITextView Crashes on iOS 17 [NSInternalInconsistencyException attempt to create NSTextRange from nil location

___exceptionPreprocess + 164
1
libobjc.A.dylib
_objc_exception_throw + 60
2
Foundation
__userInfoForFileAndLine
3
UIFoundation
-[NSTextRange initWithLocation:endLocation:] + 468
4
UIFoundation
___70-[NSTextParagraph enumerateSubstringsFromLocation:options:usingBlock:]_block_invoke + 132
5
Foundation
-[NSString enumerateSubstringsInRange:options:usingBlock:] + 1592
6
UIFoundation
-[NSTextParagraph enumerateSubstringsInRange:options:usingBlock:] + 180
7
UIFoundation
-[NSTextParagraph enumerateSubstringsFromLocation:options:usingBlock:] + 300
8
UIFoundation
___74-[NSTextLayoutManager enumerateSubstringsFromLocation:options:usingBlock:]_block_invoke_4 + 332
9
UIFoundation
-[NSTextContentStorage enumerateTextElementsFromLocation:options:usingBlock:] + 3908
10
UIFoundation
-[NSTextLayoutManager enumerateSubstringsFromLocation:options:usingBlock:] + 196
11
UIKitCore
-[_UITextKit2LayoutController rangeOfCharacterClusterAtIndex:type:] + 312
12
UIKitCore
-[UITextInputController _rangeForBackwardsDelete] + 104
13
UIKitCore
___39-[UITextInputController deleteBackward]_block_invoke + 120
14
UIKitCore
-[UITextInputController _performWhileSuppressingDelegateNotifications:] + 48
15
UIKitCore
-[UITextInputController deleteBackward] + 172
16
UIKitCore
-[UITextView deleteBackward] + 40
17
UIKitCore
-[UIKBInputDelegateManager _deleteBackwardAndNotify:reinsertText:overrideOriginalContextBeforeInputWith:] + 472
18
UIKitCore
-[UIKeyboardImpl deleteBackwardAndNotifyAtEnd:deletionCount:reinsertTextInLoop:] + 248
19
UIKitCore
-[UIKeyboardImpl performKeyboardOutput:checkingDelegate:forwardToRemoteInputSource:] + 1424
20
UIKitCore
-[UIKeyboardImpl performKeyboardOutput:forwardToRemoteInputSource:] + 32
21
UIKitCore
___77-[UIKeyboardImpl _processInputViewControllerKeyboardOutput:executionContext:]_block_invoke + 480
22
UIKitCore
-[UIKeyboardImpl _performKeyboardOutput:respectingForwardingDelegate:] + 252
23
UIKitCore
-[UIKeyboardImpl _processInputViewControllerKeyboardOutput:executionContext:] + 168
24
UIKitCore
-[UIKeyboardImpl performOperations:withTextInputSource:] + 56
25
UIKitCore
___67-[UIKeyboardImpl(UIKitInternal) _performInputViewControllerOutput:]_block_invoke + 168
26
UIKitCore
-[UIKeyboardTaskEntry execute:] + 208
27
UIKitCore
-[UIKeyboardTaskQueue continueExecutionOnMainThread] + 324
28
Foundation
___NSThreadPerformPerform + 264
29
CoreFoundation
___CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
30
CoreFoundation
___CFRunLoopDoSource0 + 176
31
CoreFoundation
___CFRunLoopDoSources0 + 244
32
CoreFoundation
___CFRunLoopRun + 828
33
CoreFoundation
_CFRunLoopRunSpecific + 608
34
GraphicsServices
_GSEventRunModal + 164
35
UIKitCore
-[UIApplication _run] + 888
36
UIKitCore
_UIApplicationMain + 340

I have a similar problem. 0 CoreFoundation 0xed5e0 __exceptionPreprocess 1 libobjc.A.dylib 0x2bc00 objc_exception_throw 2 Foundation 0x6bc398 _userInfoForFileAndLine 3 UIFoundation 0x19464 -[NSTextRange initWithLocation:endLocation:] 4 UIFoundation 0x29c78 __NSTextLayoutManagerRemoveTextLayoutFragmentsInTextRange 5 UIFoundation 0x75ad4 -[NSTextLayoutManager _invalidateLayoutForTextRange:hard:] 6 UIFoundation 0x764c0 -[NSTextLayoutManager processLayoutInvalidationForTextRange:synchronizing:] 7 CoreFoundation 0x35d94 NSARRAY_IS_CALLING_OUT_TO_A_BLOCK 8 CoreFoundation 0x35ac0 -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] 9 UIFoundation 0xa863c __54-[NSTextContentManager synchronizeTextLayoutManagers:]_block_invoke 10 CoreFoundation 0x35d94 NSARRAY_IS_CALLING_OUT_TO_A_BLOCK 11 CoreFoundation 0x35ac0 -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] 12 UIFoundation 0x19720 -[NSTextContentManager synchronizeTextLayoutManagers:] 13 UIFoundation 0x187a4 -[NSTextContentStorage synchronizeTextLayoutManagers:] 14 UIFoundation 0x11dcc -[NSTextStorage endEditing] 15 UIFoundation 0x11c20 -[NSTextStorage coordinateEditing:] 16 UIKitCore 0x10cf858 -[UITextInputController _insertText:fromKeyboard:] 17 UIKitCore 0x10d14f4 -[UITextInputController deleteBackward] 18 UIKitCore 0x10f2ea8 -[UITextView deleteBackward] 19 UIKitCore 0x933dc8 -[UIKBInputDelegateManager _deleteBackwardAndNotify:reinsertText:overrideOriginalContextBeforeInputWith:] 20 UIKitCore 0xc1c64c -[UIKeyboardImpl deleteBackwardAndNotifyAtEnd:deletionCount:reinsertTextInLoop:] 21 UIKitCore 0xc12364 -[UIKeyboardImpl performKeyboardOutput:checkingDelegate:forwardToRemoteInputSource:] 22 UIKitCore 0xc11aa8 -[UIKeyboardImpl performKeyboardOutput:forwardToRemoteInputSource:] 23 UIKitCore 0xc11a60 -[UIKeyboardImpl performKeyboardOutput:] 24 UIKitCore 0xc39bcc -[UIKeyboardImpl _performKeyboardOutput:respectingForwardingDelegate:] 25 UIKitCore 0xc100c8 __55-[UIKeyboardImpl handleKeyboardInput:executionContext:]_block_invoke.1176 26 UIKitCore 0xbf50d8 -[UIKeyboardImpl performOperations:withTextInputSource:] 27 UIKitCore 0xc0fe6c __55-[UIKeyboardImpl handleKeyboardInput:executionContext:]_block_invoke_2 28 UIKitCore 0x312464 -[UIKeyboardTaskEntry execute:] 29 UIKitCore 0x312348 -[UIKeyboardTaskQueue continueExecutionOnMainThread] 30 Foundation 0xf6064 __NSThreadPerformPerform 31 CoreFoundation 0x37acc CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION 32 CoreFoundation 0x36d48 __CFRunLoopDoSource0 33 CoreFoundation 0x354fc __CFRunLoopDoSources0 34 CoreFoundation 0x34238 __CFRunLoopRun 35 CoreFoundation 0x33e18 CFRunLoopRunSpecific 36 GraphicsServices 0x35ec GSEventRunModal 37 UIKitCore 0x22f2fc -[UIApplication _run] 38 UIKitCore 0x22e938 UIApplicationMain

Excuse me, have you found a solution?

I recently encountered this issue and successfully reproduced it.

After entering text in Korean into a UITextView, if you input multiple ✔️ emojis on the next line and insert line breaks between the emojis, they don't appear as one per line; instead, they appear two at a time. Deleting the emojis that are displayed two at a time leads to the mentioned error.

I haven't found a solution yet, but I wanted to share this information first.

I recently encountered this issue and successfully reproduced it. After entering text in Korean into a UITextView, if you input multiple ✔️ emojis on the next line and insert line breaks between the emojis, they don't appear as one per line; instead, they appear two at a time. Deleting the emojis that are displayed two at a time leads to the mentioned error. I haven't found a solution yet, but I wanted to share this information first.

func validVS16String() -> (Bool, String) { guard #available(iOS 17.0, *) else { return (true, self) } guard unicodeScalars.contains(where: { $0.properties.isVariationSelector }) else { return (true, self) } var valid = true var validScalars: [Unicode.Scalar] = [] var preScalar: Unicode.Scalar? for v in unicodeScalars { if v.properties.isVariationSelector, preScalar?.properties.isEmoji == false { valid = false } else { validScalars.append(v) } preScalar = v }

    if valid {
        return (true, self)
    }
    let validString = String(String.UnicodeScalarView(validScalars))
    return (false, validString)
}

try this

try this

     func validVS16String() -> (Bool, String) {
        guard #available(iOS 17.0, *) else {
            return (true, self)
        }
        guard unicodeScalars.contains(where: { $0.properties.isVariationSelector }) else {
            return (true, self)
        }
        var valid = true
        var validScalars: [Unicode.Scalar] = []
        var preScalar: Unicode.Scalar?
        for v in unicodeScalars {
            if v.properties.isVariationSelector, preScalar?.properties.isEmoji == false {
                valid = false
            } else {
                validScalars.append(v)
            }
            preScalar = v
        }
        
        if valid {
            return (true, self)
        }
        let validString = String(String.UnicodeScalarView(validScalars))
        return (false, validString)
    }

I've posted a workaround for this crash in this topic: https://developer.apple.com/forums/thread/748478

UITextView Crashes on iOS 17 [NSInternalInconsistencyException attempt to create NSTextRange from nil location
 
 
Q