.toInt() 在 Swift 2 中被移除了吗? [英] .toInt() removed in Swift 2?
问题描述
我正在开发一个使用文本字段并将其转换为整数的应用程序.以前我的代码
I was working on an application that used a text field and translated it into an integer. Previously my code
textField.text.toInt()
工作了.现在 Swift 将此声明为错误并告诉我这样做
worked. Now Swift declares this as an error and is telling me to do
textField.text!.toInt()
它说没有 toInt()
并尝试使用 Int()
.那也行不通.刚刚发生了什么?
and it says there is no toInt()
and to try using Int()
. That doesn't work either. What just happened?
推荐答案
在 Swift 2.x 中,.toInt()
函数已从 String
中移除.作为替代,Int
现在有一个接受 String
In Swift 2.x, the .toInt()
function was removed from String
. In replacement, Int
now has an initializer that accepts a String
Int(myString)
在你的情况下,你可以使用 Int(textField.text!)
插入 textField.text!.toInt()
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)
这篇关于.toInt() 在 Swift 2 中被移除了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!