I can’t start my SWIFT 3 timer

I used timers before and they worked, but since NSTimer was changed to Timer, my timer refused to work. The code I tried is as follows:

override func viewDidLoad() {
super.viewDidLoad()

_ = Timer(timeInterval: 3, target: self, selector: #selector (test), userInfo: nil, repeats: true)

}

func test() {
print("The timer worked")
}

The test function is never called, I don’t know why. I also tried to initialize it and use it like this:

var followUpTimer:Timer! 

override func viewDidLoad() {
super.viewDidLoad()

followUpTimer = Timer(timeInterval: 3, target: self, selector: #selector(test) , userInfo: nil, repeats: true)

}

func test() {
print("The timer worked")
}

But this doesn't work either. What am I doing wrong? All the answers I found on the Internet indicate that this is the correct approach.

(NS)Timer requires A running loop can work properly. You can add a timer to the loop programmatically, but it is more convenient to use this method

Timer.scheduledTimer(withTimeInterval:. ..

I have used timers before and they worked, but since NSTimer was changed to Timer, my timer refused to work. The code I tried is as follows:

override func viewDidLoad() {
super.viewDidLoad()

_ = Timer(timeInterval: 3, target: self, selector: #selector(test), userInfo: nil, repeats: true)

}

func test() {
print("The timer worked ")
}

The test function is never called, I don’t know why. I also tried to initialize it and use it like this:

var followUpTimer:Timer!

override func viewDidLoad() {
super.viewDidLoad()

followUpTimer = Timer(timeInterval: 3, target: self, selector : #selector(test), userInfo: nil, repeats: true)

}

func test() {
print("The timer worked")< br />}

But it doesn’t work either. What am I doing wrong? All the answers I found on the Internet indicate that this is the right approach.

(NS)Timer needs a running cycle The loop can work properly. You can add a timer to the loop programmatically, but it is more convenient to use this method

Timer.scheduledTimer(withTimeInterval:... 

Leave a Comment

Your email address will not be published.