简体   繁体   中英

SwiftUI : iOS 15 crash Invalid parameter not satisfying: pos' *** First throw call stack:

I'm using TextEditor and it shows a paste option. As soon as I tap on the paste option, the app crashes. I'm testing in a simulator. I have tested on iPhone 13 as well and the paste option is not shown, instead it shows scanText.

Below is the code what I have tried.

VStack(alignment:.center) {
TextEditor(text: $description)
    .font(.body)
    .textSelection(.enabled)
    .frame(height:(UIScreen.main.bounds.size.height / 3) - 95)
    .cornerRadius(3.0)
    .background(RoundedRectangle(cornerRadius: 4.0)
                    .stroke(Color.gray, lineWidth: 0.0))
    .keyboardType(.alphabet)
    .disableAutocorrection(true)
    .onAppear {
        UITextView.appearance().backgroundColor = .clear
    }
    .foregroundColor(.gray)

    .onChange(of: description, perform: { newValue in
        if description <= wordCount { }
        else  {
            if newValue.count > wordCount {
                description = String(newValue.prefix(wordCount))
            }else {
                self.description.removeLast()
            }
        }
        if newValue.contains("\n") {
            description = newValue.replacingOccurrences(of: "\n", with: "")
            nameIsFocused = false
        }
    })
    .background(
        RoundedRectangle(cornerRadius: 0, style: .continuous)
            .fill((colorScheme == .dark ? Color.white.opacity(0.1) :  Color.black.opacity(0.1)))
    )
    .padding(.top, -8)

}

please find crash details

 *** Assertion failure in -[_UITextKitTextPosition compare:], UITextKitTextViewEditingSupport.m:43
            2022-06-28 19:16:55.409929+0530 DemoApp[64078:1855509] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: pos'
            *** First throw call stack:
            (
                0   CoreFoundation                      0x00007fff203fbbb4 __exceptionPreprocess + 242
                1   libobjc.A.dylib                     0x00007fff2019ebe7 objc_exception_throw + 48
                2   Foundation                          0x00007fff20750c12 _userInfoForFileAndLine + 0
                3   UIKitCore                           0x00007fff253b5b68 -[_UITextKitTextPosition compare:] + 235
                4   UIKitCore                           0x00007fff253a8e77 -[UITextInputController comparePosition:toPosition:] + 85
                5   UIKitCore                           0x00007fff253ca315 -[UITextView comparePosition:toPosition:] + 85
                6   UIKitCore                           0x00007fff2536fc4d -[UITextPasteController _clampRange:] + 634
                7   UIKitCore                           0x00007fff253703c0 __87-[UITextPasteController _performPasteOfAttributedString:toRange:forSession:completion:]_block_invoke + 40
                8   UIKitCore                           0x00007fff253705c8 __87-[UITextPasteController _performPasteOfAttributedString:toRange:forSession:completion:]_block_invoke.174 + 184
                9   UIKitCore                           0x00007fff253b1cec -[UITextInputController _pasteAttributedString:toRange:completion:] + 471
                10  UIKitCore                           0x00007fff25370326 -[UITextPasteController _performPasteOfAttributedString:toRange:forSession:completion:] + 726
                11  UIKitCore                           0x00007fff2536f603 __49-[UITextPasteController _executePasteForSession:]_block_invoke + 299
                12  libdispatch.dylib                   0x000000010ef34a28 _dispatch_call_block_and_release + 12
                13  libdispatch.dylib                   0x000000010ef35c0c _dispatch_client_callout + 8
                14  libdispatch.dylib                   0x000000010ef44376 _dispatch_main_queue_callback_4CF + 1195
                15  CoreFoundation                      0x00007fff2036985d __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
                16  CoreFoundation                      0x00007fff203640ba __CFRunLoopRun + 2772
                17  CoreFoundation                      0x00007fff20363103 CFRunLoopRunSpecific + 567
                18  GraphicsServices                    0x00007fff2c851cd3 GSEventRunModal + 139
                19  UIKitCore                           0x00007fff24ffbe63 -[UIApplication _run] + 928
                20  UIKitCore                           0x00007fff25000a53 UIApplicationMain + 101
                21  SwiftUI                             0x00007fff5cb0d6a3 $s7SwiftUI17KitRendererCommon33_ACC2C5639A7D76F611E170E831FCA491LLys5NeverOyXlXpFAESpySpys4Int8VGSgGXEfU_ + 196
                22  SwiftUI                             0x00007fff5cb0d5dd $s7SwiftUI6runAppys5NeverOxAA0D0RzlF + 148
                23  SwiftUI                             0x00007fff5c4eece1 $s7SwiftUI3AppPAAE4mainyyFZ + 61
                24  DemoApp                               0x000000010d987cbe $s5DemoApp0A3AppV5$mainyyFZ + 30
                25    DemoApp                             0x000000010d987d69 main + 9
                26  dyld                                0x000000010eaaee1e start_sim + 10
                27  ???                                 0x0000000000000001 0x0 + 1
            )
            libc++abi: terminating with uncaught exception of type NSException

Thanks in advance

我认为问题在于您正在尝试更改 .onChange 修饰符中的 Textfield 文本,这会导致无限循环

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM