本文介绍了.toInt()已在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)

根据您的情况,您可以使用由textField.text!.toInt()插入的Int(textField.text!)

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()

快速2.x,3.x

let myString: String = "256"
let myInt: Int? = Int(myString)

这篇关于.toInt()已在Swift 2中删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 05:09