所以我非常擅长制作APP广告
Swift,我试图在文本字段中做一些非常简单的输入,取值,然后将它们用作Int进行某些计算.
但是’var distance’有些东西不正确
var handicapTal:Int var MagicNumber:Int var handicap = inputHCP.text.toInt() var distance = inputdistance.text.toInt() if (handicap >= 0 && handicap <= 20) { handicapTal = 30 } else if(handicap > 20 && handicap <= 40){ handicapTal = 10 } MagicNumber = distance - handicapTal
它是导致错误的最后一行代码.它说致命错误:无法解开Optional.None
解决方法
在你的例子中,距离是一个Int?否则称为可选的Int. .toInt()返回Int?因为从String到Int的转换可能失败.请参阅以下示例:
Welcome to Swift! Type :help for assistance. 1> let a = "12" a: String = "12" 2> let b = a.toInt() b: Int? = 12 3> let c = "Hello" c: String = "Hello" 4> let d = c.toInt() d: Int? = nil 5> if let e = a.toInt() { println("e = \(e)") } e = 12 6> if let f = c.toInt() { println("Huh?") } 7>