Ios 用int添加字符串
我在Xcode7上遇到了一个问题,我想从文本框中添加一些数字,其中包含一个我已经有的整数,但这段代码似乎不起作用: @iAction func ConvertAction(发送方:AnyObject) { //这给了我一个错误,即“二进制运算符”+“不能应用于string?和Int类型的操作数” 这应该起作用:Ios 用int添加字符串,ios,swift2,xcode7,Ios,Swift2,Xcode7,我在Xcode7上遇到了一个问题,我想从文本框中添加一些数字,其中包含一个我已经有的整数,但这段代码似乎不起作用: @iAction func ConvertAction(发送方:AnyObject) { //这给了我一个错误,即“二进制运算符”+“不能应用于string?和Int类型的操作数” 这应该起作用: let Celcius:String = Textinput.text if let celc = Int(Celcius) { let fahrenheit:Int = (
let Celcius:String = Textinput.text
if let celc = Int(Celcius) {
let fahrenheit:Int = ( celc + 160 )
Labeloutput.text"\(celc) Celcius is \(fahrenheit) Fahrenheit"
} else {
print("Computer says no!")
}
当然,除非输入不能转换为整数,否则会出现异常thrown@Paulw11你完全正确。最初的答案是假设只键入数字(可能使用numberPad)不过,我已经更新了答案,使其更加可靠。不过,我相信可能有几种方法可以解决这个问题。谢谢。谢谢你,Paulw11!成功了!!@St33v3n不客气-请将其标记为已接受的答案。
//print on label the farenheit
Labeloutput.text = "\(Celsius) Celcius is /(fahrenheit) Fahrenheit"
}
let Celcius:String = Textinput.text
if let celc = Int(Celcius) {
let fahrenheit:Int = ( celc + 160 )
Labeloutput.text"\(celc) Celcius is \(fahrenheit) Fahrenheit"
} else {
print("Computer says no!")
}