Swift How to get integer from string and convert it into integer

Swift 3/4

let string = "0kaksd020dk2kfj2123"
if let number = Int(string.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()) {
    // Do something with this number
}

You can also make an extension like:

extension Int {
    static func parse(from string: String) -> Int? {
        Int(string.components(separatedBy: CharacterSet.decimalDigits.inverted).joined())
    }
}

And then later use it like:

if let number = Int.parse(from: "0kaksd020dk2kfj2123") { 
    // Do something with this number
} 

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)