I have a Swift class that I'd like to look something like this:
class UpdateManager {
let timer: NSTimer
init() {
timer = NSTimer(timeInterval: 600, target: self, selector: "check", userInfo: nil, repeats: true)
}
func check() {
// Do some stuff
}
}
However, Swift doesn't like the fact that I'm passing self to the NSTimer initializer. Am I breaking some pattern here? How is one supposed to accomplish initialization like this?
You've found the primary use case of the Implicitly Unwrapped Optional.
self from init, before timer is initialized.timer should never be nil, so you shouldn't have to check it outside of init.So, you should declare let timer: NSTimer!.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With