NSTaggedPointerString

Dear Team,

We are receiving the below crash from Crashlytics in recent times,

Could you please confirm on the reason behind and how that can be avoided

Fatal Exception: NSInvalidArgumentException
-[NSTaggedPointerString stringByReplacingCharactersInRange:withString:]: nil argument

0 CoreFoundation __exceptionPreprocess
1 libobjc.A.dylib objc_exception_throw
2 Foundation

+[NSUserDefaults(NSUserDefaults_NSURLExtras) _web_defaultsDidChange]

3 TextInput
-[TIDocumentState(StateTransitions) documentStateAfterReplacingText:withText:]

40 UIKitCore
UIApplicationMain

41 main + 33

42 libdyld.dylib start

A quick help will be much appreciated.
Could you please show the code and tell where exactly the crash occurs ?
You probably passed an argument that was nil to some function which cannot accept it or apply to a nil String.
Most likely when you call
Code Block
func replacingCharacters(in range: NSRange, with replacement: String) -> String { }

Thanks Claude.

Please find the code snippets. We have done the implementation in different place added here for your view.

Scenario #1
   func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
    let nsString = NSString(string: textField.text!)
    let newText = nsString.replacingCharacters(in: range, with: string)
}

Scenario #2
   func stringByReplacingFirstOccurrenceOfString(target: String, withString replaceString: String) -> String {
    if let range = self.range(of: target) {
      return replacingCharacters(in: range, with: replaceString)
    } else {
      return self
    }
  }
NSTaggedPointerString is an implementation detail here [1]. The exception is caused by the placement string being nil. It’s hard to be sure exactly how you got into that state. Do you have an Apple crash report for this? If so, please post here (use the text attachment feature, the paperclip icon, to avoid clogging up the timeline).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] If you’re curious, you can read mikeash.com/pyblog/friday-qa-2015-07-31-tagged-pointer-strings.html. The details have changed a bit over the years but the basic concepts still apply.
NSTaggedPointerString
 
 
Q