这是我的代码,我是使用swift的初学者而我的代码不起作用,应用程序应该取'ageInput'textField的值并将其乘以7然后在resultLabel中显示结果,我总是得到错误:
无法将类型为'int'的值分配给'String?'
类型的值class ViewController: UIViewController {
@IBOutlet weak var resultLabel: UILabel!
@IBOutlet weak var ageInput: UITextField!
@IBAction func findAge(sender: AnyObject) {
var catAge = ageInput.text.toInt() ?? 0
catAge = catAge * 7 ?? 0
resultLabel.text = catAge
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
我哪里出错?
答案 0 :(得分:19)
尝试:
resultLabel.text = String (catAge)
答案 1 :(得分:10)
你也可以这样做:
resultLabel.text = "\(catAge)"
答案 2 :(得分:2)
这里我有托盘使用示例解决您的问题,我希望您的问题能够解决..
var stringNumber = "1234"
var numberFromString = stringNumber.toInt()
println(numberFromString)
注意toInt():
If the string represents an integer that fits into an Int, returns the corresponding integer.