Countdown app using timer does not work

Asked 2 months ago, Updated 2 months ago, 5 views

I wanted to create a riddle app, so I wanted to add a countdown function, so I looked it up and wrote it down, but it didn't work for some reason.Is it possible to count on the label?
If possible, please let me know the code.Please.

import UIKit

classViewController:UIViewController {

    @IBOutlet weak var countLabel:UILabel!



    var timeCount = 100
    varmyTimer=Timer()

    @objc functimerUpdate(){
        timeCount = timeCount-1
        countLabel.text=String(timeCount)
        if timeCount == 0 {
            myTimer.invalidate()
        }
       func viewDidLoad(){
        super.viewDidLoad()
            myTimer=Timer.scheduledTimer(timeInterval:1.0, target:self, selector:#selector(ViewController.timerUpdate), userInfo:nil, repeat:true)


        }
    }

}

swift xcode

2022-09-30 13:53

1 Answers

As soon as you reindent the entire code correctly, your viewDidLoad() is completely included in the definition of the timerUpdate() method.

@objc functimerUpdate(){
    // This is the timerUpdate() method
    //...
    func viewDidLoad(){
        super.viewDidLoad()
        myTimer=Timer.scheduledTimer(timeInterval:1.0, target:self, selector:#selector(ViewController.timerUpdate), userInfo:nil, repeat:true)
    }
    // This is still in the timerUpdate() method
}

Swift allows this nested function definition as a language specification, but the nested function definition inside is not considered an instance method and is only a valid local function within the outer method.

Therefore, it is determined that the original viewDidLoad() does not exist in your code (as an instance method), and the viewDidLoad() inside (nested) is also not called.

(It shouldn't be too hard to issue a "some local functions are not in use" warning around here…)

Anyway, as a result, your myTimer is not configured with a correctly scheduled Timer and has a Timer instance (made with an initializer init() that is not documented how it behaves).Naturally, nothing happens that you try to schedule.

One of the reasons these errors are hard to find is that your property myTimer declaration is the code for a typical bad practice.

 varmyTimer=Timer()

Unless you clearly understand what the Timer() instance is, you should never such a property declaration.(Unfortunately, such a very bad way of writing often appears at the top of the search results.)

 var myTimer: Timer?

If you declare as shown in , you will quickly notice that the myTimer that should have been initialized remains nil.

If you rewrite your code, including that, it will look like this

import UIKit

classViewController:UIViewController {

    @IBOutlet weak var countLabel:UILabel!

    var timeCount = 100
    varmyTimer: Timer?//### You should never initialize a variable with a "useless instance"

    @objc functimerUpdate(){
        timeCount = timeCount-1
        countLabel.text=String(timeCount)
        if timeCount == 0 {
            myTimer?.invalidate()
            MyTimer=nil//###Invalidate() Timer cannot be reused, so I will make it nil.
        }
    }

    override func viewDidLoad() {//### Correct viewDidLoad() results in error without override
        super.viewDidLoad()
        //### After specifying self for target, start #selector with self.
        myTimer=Timer.scheduledTimer(timeInterval:1.0, target:self, selector:#selector(self.timerUpdate), userInfo:nil, repeat:true)
    }
}

Other things to check, my current recommended writing style, etc. are indicated by comments with ##.Please check the contents before trying.


2022-09-30 13:53

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.