61

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?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Amit Kalra
  • 4,085
  • 6
  • 28
  • 44

5 Answers5

121

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)
Jojodmo
  • 23,357
  • 13
  • 65
  • 107
  • how exactly would I put this to use? mine is .text.toInt() I don't actually have any text there, what would I put inside the Int() ? – Amit Kalra Jun 09 '15 at 18:05
  • @AmitNivedanKalra You could use `Int(.text)` – Jojodmo Jun 09 '15 at 18:06
  • 5
    @Jojodmo you should avoid forcing unwrapping. You should use let myInt = Int(myString) ?? 0 – Leo Dabus Jun 09 '15 at 18:15
  • 1
    I explicitly told it to do Int(lolField.text!) that did the trick. – Amit Kalra Jun 17 '15 at 23:41
  • 1
    The problem with `Int(textField.text!)` is that if `textField.text` is `nil` then you get an error. – alondono Sep 29 '15 at 11:07
  • @alondono If you don't know what the input will be, you can use `Int(textField.text ?? "0") ?? 0` – Jojodmo Sep 29 '15 at 14:15
  • 1
    @Jojodmo Why did you use myInt: Int? when you know your input is a string from line above. Wouldn't this be enough? let myInt = Int(myString). I am just trying to understand. – Sam B Oct 09 '15 at 16:38
  • @SamB Int(String) is a failable initializer. Although you do know the input string and using Int("256")! would be fine, for the sake of example it isn't unwrapped, because in reality you won't know if the input is a number or not – Jojodmo Oct 09 '15 at 17:02
4

Swift 2

let myString: NSString = "123"
let myStringToInt: Int = Int(myString.intValue)

declare your string as an object NSString and use the intValue getter

toddsalpen
  • 411
  • 4
  • 9
  • 1
    well you have that solution but, i am an objective-c developer as well and for me is so familiar use NS object, but your idea is cool. :) – toddsalpen Apr 13 '16 at 17:33
1

Its easy enough to create your own extension method to put this back in:

extension String {
    func toInt() -> Int? {
        return Int(self)
    }
}
Nathan
  • 11,938
  • 12
  • 55
  • 62
0

I had the same issued in Payment processing apps. Swift 1.0

let expMonth = UInt(expirationDate[0].toInt()!)
let expYear = UInt(expirationDate[1].toInt()!)

After in Swift 2.0

let expMonth = Int(expirationDate[0])
let expYear = Int(expirationDate[1])
Swikar
  • 11
  • 6
-1

That gave me some errors too!

This code solved my errors

let myString: String = dataEntered.text!  // data entered in textField
var myInt: Int? = Int(myString)    // conversion of string to Int

myInt = myInt! * 2  // manipulating the integer sum,difference,product, division 

finalOutput.text! = "\(myInt)"  // changes finalOutput label to myInt
Jojodmo
  • 23,357
  • 13
  • 65
  • 107
ЯOMAИ
  • 21
  • 2