Invalid Numeric Value (NaN) Error in SwiftUI's TextField on Long-Press

I'm experiencing a peculiar issue with SwiftUI's TextField. Whenever I long-press on the TextField, the console outputs an error about passing an invalid numeric value (NaN, or not-a-number) to the CoreGraphics API. This issue persists even in a new Xcode project with minimal code.

Code Snippet:

import SwiftUI

struct ContentView: View {
    @State private var text: String = ""

    var body: some View {
        TextField("Placeholder", text: $text)
    }
}
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.

Steps to Reproduce:

Create a new SwiftUI project in Xcode. Add a TextField to the ContentView. Run the app on a device or simulator. Long-press inside the TextField. What I've Tried:

  • Updating to the latest version of Xcode and iOS.
  • Using UIViewRepresentable to wrap a UIKit UITextField.
  • Creating a new Xcode project to isolate the issue.
  • None of these steps have resolved the issue.

Questions:

  • Has anyone else encountered this problem?
  • Are there any known workarounds for this issue?
  • Is this a known bug, and if so, has it been addressed in any updates?
Post not yet marked as solved Up vote post of Poulsen89 Down vote post of Poulsen89
4.1k views
  • I'm encountering this as well. I wasn't sure what was tripping the error, but after seeing your post, I can confirm that it's a long press.

Add a Comment

Replies

I am seeing the same problem. This also happens in UIKit. A "long press" on the TextField reproduces the problem. Adding a UITextField inside a View is enough the reproduce the problem. I think this is a bug that Apple should address?

  • I have the exact problem you mention.

  • I'm hoping (And think) this is just a bug, I tried running Xcode on another computer, and created a new project, and used the text field and tested it on my iPhone, and found the same messages coming up in the consul. I never had this problem before, but I'm new to this!

Add a Comment

I have the same issue .. however .. while it does occur on a long press, it also occurs anytime I type in the textfield. Occurs with minimal code example, in simulator and on device - using both on-screen keyboard and hardware keyboard. Driving me insane!!

Adding CG_NUMERICS_SHOW_BACKTRACE to Environment Variables as the error suggests, provides the following backtrace. I don't find it very informative - but perhaps it can provide some clues to other more intelligent individuals:

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.
Backtrace:
  <CGPathMoveToPoint+84>
   <+[UIBezierPath _continuousRoundedRectBezierPath:withRoundedCorners:cornerRadii:segments:smoothPillShapes:]+1120>
    <+[UIBezierPath _continuousRoundedRectBezierPath:withRoundedCorners:cornerRadius:segments:]+180>
     <+[UIBezierPath _roundedRectBezierPath:withRoundedCorners:cornerRadius:segments:legacyCorners:]+340>
      <-[_UITextChoiceAccelerationBubble backgroundImageView]+156>
       <-[_UITextChoiceAccelerationBubble anchorToTextIfNeeded]+500>
        <-[_UITextChoiceAccelerationBubble updateTextAnchorForParentView:]+156>
         <-[_UITextChoiceAccelerationBubble updateTextBoxHighlightForRect:inTextView:parentView:highlightColor:]+88>
          <-[_UITextChoiceAccelerationAssistant updateActivePromptForCandidate:displayRects:highlightOnly:]+352>
           <-[UIKeyboardImpl updateAutocorrectPrompt:correctionRects:]+492>
            <-[UIKeyboardImpl updateAutocorrectPrompt:executionContext:]+608>
             <__45-[UIKeyboardImpl touchAutocorrectPromptTimer]_block_invoke+112>
              <__56-[UIKeyboardScheduledTask handleDeferredTimerFiredEvent]_block_invoke+184>
               <-[UIKeyboardTaskEntry execute:]+200>
                <-[UIKeyboardTaskQueue continueExecutionOnMainThread]+304>
                 <-[UIKeyboardTaskQueue addTask:]+92>
                  <-[UIKeyboardScheduledTask handleDeferredTimerFiredEvent]+216>
                   <__invoking___+144>
                    <-[NSInvocation invoke]+276>
                     <-[_UIActionWhenIdle invoke]+52>
                      <__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__+32>
                       <__CFRunLoopDoObservers+528>
                        <__CFRunLoopRun+968>
                         <CFRunLoopRunSpecific+572>
                          <GSEventRunModal+160>
                           <-[UIApplication _run]+868>
                            <UIApplicationMain+124>
                             <OUTLINED_FUNCTION_70+500>
                              <OUTLINED_FUNCTION_70+148>
                               <OUTLINED_FUNCTION_2+92>
                                <$s16FurtherIsolation0aB3AppV5$mainyyFZ+40>
                                 <main+12>
                                  104d81558
                                   104e62058                                    3125800000000000

On staring at the backtrace, and seeing continuousRoundedRectBezierPath:withRoundedCorner and UITextMagnifiedLoupeView makes me think the error occurs whenever the new "pill shaped" text selection magnifier appears (long pressing is one case in which this text selection magnifier appears).

I think this is a bug on Apple's side. But surprised there is not more comments on the forums / StackOverflow, etc about this.

I can create a workaround to the issue by preventing the default long press and autocorrection behaviours (which will cause magnifiers or correction suggestions).

For me at least, this stops the warning (but - obviously - prevents text selection and autocorrection).

struct ContentView: View {
    @State private var text: String = ""
    @FocusState private var textfieldFocused: Bool
    
    var body: some View {
        TextField("Placeholder", text: $text)
            .focused($textfieldFocused)
            .onLongPressGesture(minimumDuration: 0.0) {
                textfieldFocused = true
            }
            .autocorrectionDisabled()
    }
}
  • Thanks @mark-e-mark ! Based on your fix the below seems to work and limits its impact.

    TextField("Placeholder", text: $text)
    #if DEBUG
    	.autocorrectionDisabled()
    #endif
    
Add a Comment

Report filed via Feedback Assistant: FB13276843

Please file reports too - so that we can can some action on this. :)

  • Hi! Could you solve this?

  • My FB13604398 is the new case hopefully It will help

Add a Comment

I'm getting a similar message in the console but doing something completely unrelated. I am using the latest API of SwiftCharts with .ChartScrollableAxes(). On My chart, I'm adding a .annotation() that appears when tapping on a point in the chart. If I have the annotation showing and then I change the scale of my chart (say tapping 'year' scale instead of 'month' scale, then I get the console message.

Very weird and unrelated but I'll file a report too.

Console message: "[Unknown process name] 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. [Unknown process name] If you want to see the backtrace, please set CG_NUMERICS_SHOW_BACKTRACE environmental variable."

iOS 17.2, Xcode Version 15.1 The problem has not yet been resolved.


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.

iOS 17.2 Xcode 15.1 This is happening for me on typing in textFields that have the textFieldStyle .roundedBorder applied. This is a system defined textFieldStyle so it looks like apple has a bug on their hands somewhere.

If i remove this modifier the warnings stop.

  • I see this too, in 17.4/15.3. I filed FB13687719

Add a Comment