import UIKit
class ViewController: UIViewController {
//textField do valor A
@IBOutlet weak var valueA: UITextField!
//textField do valor B
@IBOutlet weak var valueB: UITextField!
//textField do valor
@IBOutlet weak var valueC: UITextField!
//declarando as labels string que serão transformadas em int
var valueBint: Int = 0
var valueAint: Int = 0
var valueCint: Int = 0
//valor de delta em int
var deltaValue: Int = 0
@IBAction func calcularButton(sender: UIButton) {
let valueAint = Int(valueA.text!)
let valueBint = Int(valueB.text!)
let valueCint = Int(valueC.text!)
//conta do delta
deltaValue = (valueBint! * valueBint!) - (4 * valueAint! * valueCint!)
print(deltaValue)
}
@IBOutlet weak var deltaValueS: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
deltaValueS.text = deltaValue.description
}
}
这是我的代码。我希望int
deltaValue
在deltaValueS
标签中显示为字符串。我尝试了很多事情,但没有显示该值。我试过了:
deltaValueS.text = "∆ is \(deltaValue)"
deltaValueS.text = "∆ is" + String(deltaValue)
deltaValueS.text = deltaValue.description
但是没有办法。问题不在数学上,数字会为我打印出来,而
deltaValue
是正确的。谁能帮我?
最佳答案
您可以使用属性观察器轻松实现此目的。
每当deltaValue更改时,这将自动设置文本。
尝试这个:
var deltaValue: Int = 0 {
didSet {
self.deltaValueS.text = "\(deltaValue)"
}
}