I was working on an application that used a text field and translated it into an integer. Previously my code
textField.text.toInt()
worked. Now Swift declares this as an error and is telling me to do
textField.text!.toInt()
and it says there is no toInt()
and to try using Int()
. That doesn't work either. What just happened?
In Swift 2.x, the .toInt()
function was removed from String
. In replacement, Int
now has an initializer that accepts a String
Int(myString)
In your case, you could use Int(textField.text!)
insted of textField.text!.toInt()
Swift 1.x
let myString: String = "256" let myInt: Int? = myString.toInt()
Swift 2.x, 3.x
let myString: String = "256" let myInt: Int? = Int(myString)
let myString: NSString = "123" let myStringToInt: Int = Int(myString.intValue)
declare your string as an object NSString and use the intValue getter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With