我在Xcode7上遇到问题我想在文本框中添加一些我已经拥有的整数但是这段代码似乎无法正常工作:
@IBAction func ConvertAction(sender:AnyObject) {
//get the user input
let Celcius:String? = Textinput.text
//here
//here is the problem
// convert celcius to fahrenheit
let fahrenheit:Int = ( Celcius + 160 )
//这给了我一个错误说"二元运算符" +"不能应用于字符串类型的操作数?和Int"
//print on label the farenheit
Labeloutput.text = "\(Celsius) Celcius is /(fahrenheit) Fahrenheit"
}
答案 0 :(得分:0)
这应该有效:
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!")
}