警告:线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)
import UIKit
class ViewController: UIViewController {
var count = 0
var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//Label
var label = UILabel()
label.frame = CGRect(x: 150, y: 150, width: 60, height: 60)
label.text = "0"
self.view.addSubview(label)
//Button
var button = UIButton()
button.frame = CGRect(x: 150, y: 250, width: 60, height: 60)
button.setTitle("Click", for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
self.view.addSubview(button)
button.addTarget(self, action: #selector(ViewController.incrementCount), for: UIControlEvents.touchUpInside)
}
@objc func incrementCount() {
self.count = self.count + 1
self.label.text = "\(self.count)" //here got the warning
}
}