Search code examples
stringswift2xcode7

How to check if a string only contains numbers in Swift 2?


Before Swift 2 I used this extension to check if a string only is made out of numbers:

func isNumbersOnly() -> Bool {
        let regexNumbersOnly = NSRegularExpression(pattern: ".*[^0-9].*", options: nil, error: nil)!
        return regexNumbersOnly.firstMatchInString(self, options: nil, range: NSMakeRange(0, self.length())) != nil
}

but now with Swift 2 I get the error

Cannot invoke initializer for type 'NSRegularExpression' with an argument list of type '(pattern: String, options: NilLiteralConvertible, error: NilLiteralConvertible)'

Is there a better known way now? Thnx!


Solution

  • In Swift 2 NSRegularExpression "throws" so you have to use it with try.

    Also you can't pass nil for options anymore: if you don't want to specify options, pass an empty array (same for firstMatchInString).

    And self.length() should become self.characters.count.

    Last note: if the goal is to determine if a String contains only numbers, and since you're naming it "isNumbersOnly", the resulting Boolean should be true if there's only numbers: it's currently the inverse. I've fixed this in my example.

    Ignoring errors:

    let regexNumbersOnly = try! NSRegularExpression(pattern: ".*[^0-9].*", options: [])
    return regexNumbersOnly.firstMatchInString(self, options: [], range: NSMakeRange(0, self.characters.count)) == nil
    

    With proper error handling:

    do {
        let regexNumbersOnly = try NSRegularExpression(pattern: ".*[^0-9].*", options: [])
        return regexNumbersOnly.firstMatchInString(self, options: [], range: NSMakeRange(0, self.characters.count)) == nil
    } catch let error as NSError {
        print(error.description)
    }