Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 计时器类,不增加计数_Swift_Xcode - Fatal编程技术网

Swift 计时器类,不增加计数

Swift 计时器类,不增加计数,swift,xcode,Swift,Xcode,我不确定我哪里做错了。我没有看到任何错误,但计数没有增加 @IBOutlet weak var myTimerLabel: UILabel! let myTimer = Timer() @objc func myTestFunc() { var count = 0 count+=1 myTimerLabel.text = String(count) } override func viewDidLoad() { Timer.schedu

我不确定我哪里做错了。我没有看到任何错误,但计数没有增加

@IBOutlet weak var myTimerLabel: UILabel!

let myTimer = Timer()    

@objc func myTestFunc() {
   var count = 0
   count+=1
   myTimerLabel.text = String(count)
}

override func viewDidLoad() {

             Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector  
            (myTestFunc), userInfo: nil, repeats: true)
     }

它不会增加,因为每次调用myTestFunc()时,它都会用零重新初始化count变量。因此,在myTestFunc()之外声明count变量。

它不会增加,因为每次调用myTestFunc()时,它都会用零重新初始化count变量。所以,在myTestFunc()之外声明count变量。

您必须以正确的方式创建结构

选中此项:

@IBOutlet weak var myTimerLabel: UILabel!

let myTimer = Timer()    
var count = 0

//MARK: - view did load
override func viewDidLoad() {
  Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector (myTestFunc), userInfo: nil, repeats: true)
 }

 //TODO:- here implement your funtion 
 @objc func myTestFunc() {
 count+=1
 myTimerLabel.text = String(count)

 // if you want to stop your timer
 myTimer.invalidate()
}

祝你好运

你必须以正确的方式构建结构

选中此项:

@IBOutlet weak var myTimerLabel: UILabel!

let myTimer = Timer()    
var count = 0

//MARK: - view did load
override func viewDidLoad() {
  Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector (myTestFunc), userInfo: nil, repeats: true)
 }

 //TODO:- here implement your funtion 
 @objc func myTestFunc() {
 count+=1
 myTimerLabel.text = String(count)

 // if you want to stop your timer
 myTimer.invalidate()
}
祝你好运