.toInt () removed in Swift 2?

I was working on an application that used a text box and translated it into a whole. Earlier my code

textField.text.toInt() 

worked. Now Swift announces this as an error and tells me to do

 textField.text!.toInt() 

and he says that there is no toInt() and try using Int() . This does not work either. What just happened?

+58
swift swift2
Jun 09 '15 at 17:58
source share
5 answers

In Swift 2.x, the .toInt() function .toInt() been removed from String . Instead, Int now has an initializer that takes a String

 Int(myString) 

In your case, you can use Int(textField.text!) Insted 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) 
+121
Jun 09 '15 at 18:02
source share

Swift 2

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

declare your string as an NSString object and use intValue getter

+4
Mar 02 '16 at 19:21
source share

I had the same thing as in payment processing applications. 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]) 
0
Mar 17 '16 at 5:05
source share

Its easy enough to create your own extension method to return it:

 extension String { func toInt() -> Int? { return Int(self) } } 
0
Feb 03 '17 at 0:52
source share

It also gave me some errors!

This code resolved 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 
-one
Dec 07 '15 at 13:30
source share



All Articles