0

I am testing some strings such as one, two, three, seven, etc. to see if they are numbers using the code below. It works okay if the string actually is a number but is crashing when the string is not a number as in the word "savings".

The whole point of the method is to find out if the string can be represented as a number so I don't know how to exclude cases where it is not--if indeed that is the problem. I think that's the problem but I guess I could also be missing something as I'm weak on optionals. The string I am running this on that is producing the error is word "savings".

Here is my code:

public extension NSString {
    public var asNum: NSNumber {
               // let stringValue = String(value: self)
                let stringValue = self
                let formatter = NumberFormatter()
                formatter.isLenient = true
                formatter.numberStyle = .spellOut
                let num = formatter.number(from: stringValue as String)!///EXCEPTION
                return num
            }
        }

The exception reads:

Fatal error: Unexpectedly found nil while unwrapping an Optional value

In the debugger, I can verify that the stringValue is "savings"

Thanks for any suggestions.

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
user6631314
  • 1,751
  • 1
  • 13
  • 44
  • 2
    if string value is equal to "savings" what would you expect to happen forcing to unwrap the result? – Leo Dabus Mar 18 '19 at 02:09
  • 1
    try `return formatter.number(from: stringValue) ?? 0` – Leo Dabus Mar 18 '19 at 02:11
  • coalescing prevents the exception: return formatter.number(from: stringValue as String) ?? 0 The only problem is 0 is misleading as it could be interpreted as zero which comes up a lot in this use case. I guess I am stuck with picking some weird unlikely number... – user6631314 Mar 18 '19 at 02:37
  • 2
    The standard approach in Swift would be returning optional number. `private var number: NSNumber? {` – Leo Dabus Mar 18 '19 at 02:48
  • 1
    Btw you shouldn't declare the number formatter inside your computed property. This will create a new formatter every time you call this property – Leo Dabus Mar 18 '19 at 02:50
  • The method is called from an Objective-c file. I don't think it can handle optionals. Is that correct? – user6631314 Mar 18 '19 at 02:51
  • I know nothing about mixing obj-c and Swift – Leo Dabus Mar 18 '19 at 02:52
  • The code is actually in an extension. Where other then where it is could instantiate the formatter? – user6631314 Mar 18 '19 at 02:54
  • https://stackoverflow.com/a/27705739/2303865 – Leo Dabus Mar 18 '19 at 02:56
  • Yes, Objective-C can handle an optional `NSNumber`. It will be returned as `nil`. – rmaddy Mar 18 '19 at 03:02
  • you should use `let num = formatter.number(from: stringValue as String) ?? 0` instead of that code – mohsen Mar 18 '19 at 05:21
  • As Leo Dabus mentioned, you shouldn't create the number formatter in the computed property. Number formatters are extremely expensive. Create one elsewhere and reuse it. – clarus Mar 19 '19 at 03:13

0 Answers0