[Fixed] .searchable SwiftUI on IOS 17 cause an error: invalid numeric value – Swiftui

by
Maya Patel
ios-simulator ios17 iphone swiftui-ontapgesture xcode15

Quick Fix: To resolve the "invalid numeric value" error while using the .searchable modifier in SwiftUI on iOS 17, disable autocorrection using the .autocorrectionDisabled() modifier.

The Solutions:

Solution 1: Disable autocorrection

As per the discussion on the Apple developer forum, it is suggested to disable autocorrection to resolve the error “this application, or a library it uses, has passed an invalid numeric value (NaN, or not-a-number) to CoreGraphics API and this value is being ignored. Please fix this problem.” This fix seems to work in the meantime, until an official fix is provided.

To disable autocorrection in your SwiftUI code, you can use the .autocorrectionDisabled() modifier. Here’s an example:

struct ContentView: View {
    @State private var searchText = ""

    var body: some View {
        NavigationStack {
            Text("Searching for \(searchText)")
                .navigationTitle("Searchable Example")
        }
        .searchable(text: $searchText)
        .autocorrectionDisabled() // Disable autocorrection
    }
}

#Preview {
    ContentView()
}

By adding the .autocorrectionDisabled() modifier, autocorrection will be disabled when the user enters text in the search field. This may help to resolve the error you are encountering.

Q&A

How to fix error: this application, or a library it uses, has passed an invalid numeric value (NaN, or not-a-number) to CoreGraphics API?

Try to disable autocorrection using .autocorrectionDisabled()

Does the error occur only on simulators with iOS 17?

Yes, the error occurs only on simulators with iOS 17.

Is there an official fix from Apple yet?

No, there is no official fix from Apple yet.