I’m a beginner in Swift and have a task to change the bottom sheet message when the process of the app doesn’t work in three minutes. So, the message will change from "available" to "not available" if the process does not work.
I found code syntax like:
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)
What I think:
var waktu = 0
Timer.scheduledTimer(withTimeInterval: 180.0, repeats: false) {
if waktu == 180 {
timer.invalidate()
//run the change message function
}
}
2
Answers
Your code creates a timer that will fire once in 3 minutes.
You’re using a variation on the timer method you list,
scheduledTimer(withTimeInterval:repeats:block:)
That variant takes a closure instead of a selector. Then you’re using “trailing closure syntax” to provide that closure in braces outside the closing paren for the function call. That’s all good.
However, you define a variable
waktu
And give it a starting value of 0. You don’t show any code that will change that value, so your if statementif waktu == 180
will never evaluate to true and run your change message function. You need to update that if statement to determine if “the process of the app works”, whatever that means. (Presumably you have some definition of what your app working/not working means, and can update that if statement accordingly.)You can add observer on your property, so it will invalidate the timer when condition met, like so: