简体   繁体   English

在Swift 2,Xcode 7 beta中,如何使用if let-toInt无法将可选文本输入转换为Int

[英]How do you convert optional text input to Int in Swift 2, Xcode 7 beta using if let - toInt does not work

I'm having trouble converting optional input String to Int in order to do calculations on it. 我在将可选输入String转换为Int以便对其进行计算时遇到麻烦。

let odoField = UITextField() // allows entry of text to iOS field
odoField.text = "12500" // simulated input
let odoString = odoField.text

// now here is where I get trouble...

if let odoInt = odoString.toInt() {
    distance = Double(odoInt)
}

Apparently the toInt suffix is no longer part of Swift. 显然,toInt后缀不再是Swift的一部分。 I have tried the following: 我尝试了以下方法:

if let odoInt = Int(odoString)() {

But then I get the error "Optional type String? is not unwrapped" and a suggestion to put a ! 但随后出现错误“可选类型的字符串?未解包”并提出了一个建议! or ?, as in: 或?,如:

if let odoInt = Int(odoString!)() {

But then I STILL get the euro about unwrapping, with the suggestion that I add yet another !, then when I do that, another error that I get rid of the parens, like this: 但是然后我仍然得到有关解包的欧元,建议我再添加一个!,然后在执行此操作时,我摆脱了括号的另一个错误,如下所示:

if let odoInt = Int(odoString!)! {

And then I get ANOTHER error that "Initializer for conditional binding must have Optional type, not 'Int'." 然后出现另一个错误,“用于条件绑定的初始化程序必须具有可选类型,而不是'Int'。”

I'm trying to create conditional unwrapping, here. 我正在尝试在此处创建条件展开。

Help! 救命!

First thing to understand is that UITextField.text returns an optional string, so in your code, odoString is of type String? 首先要了解的是,UITextField.text返回一个可选字符串,因此在您的代码中, odoStringString?类型的String? . Also, keep in mind that the Int constructor takes a String , not a String? 另外,请记住, Int构造函数采用String而不是String? so you have to unwrap the String? 所以你必须解开String? before you can use it. 才能使用它。 Just putting a ! 只是放一个! after the variable (as in Int(odoString!) ) will crash your app if the odoString is nil . 如果odoStringnil则该变量(如Int(odoString!) )之后将使您的应用程序崩溃。 Better would be something like this: 更好的是这样的:

if let s = odoString, odoInt = Int(s) {
    // odoInt is of type Int. It is guaranteed to have a value in this block
}

I've tested Daniel T's answer and it worked. 我已经测试了Daniel T的答案,并且有效。

I have a situation where I want to get the result of a text field back as an optional Int. 我有一种情况,我想将文本字段的结果作为可选的Int返回。 You can extend this to cover your case using the following code: 您可以使用以下代码将其扩展为涵盖您的情况:

let odoInt = odoField.text != nil ? Int(odoField.text!) : nil
if let odoInt = odoInt {
    // Use unwrapped odoInt here
}

对于更紧凑的解决方案,另一种选择是使用flatMap:

let number = odoString.flatMap { Double($0) } ?? 0.0

In fact, it appears that the answer in Swift 2 (Xcode 7 beta 6) is simpler than anything above. 实际上,看来Swift 2(Xcode 7 beta 6)的答案比上面的答案要简单。 The code does not choke on a nil value for odoString when I do eg the following: 当执行以下操作时,代码不会阻塞odoString的nil值:

if let odoInt = Int(odoString!) {
    distance = Double(odoInt)
}

I therefore surmise, barring deeper knowledge to the contrary, that the compiler does treat this as "if the statement is True (the right side is valid), then define and initialize the variable, and continue with execution." 因此,我推测,除非有更深的了解,否则编译器确实将其视为“如果语句为True(右侧有效),则定义并初始化变量,然后继续执行”。 I welcome further feedback. 我欢迎进一步的反馈。 This does render unnecessary a lot of the extra code that is suggested above. 这确实不必要了上面建议的许多额外代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM