Initializing Swift properties that require “self” as an argument

后端 未结 3 660
一向
一向 2020-12-11 02:45

I have a Swift class that I\'d like to look something like this:

class UpdateManager {
  let timer: NSTimer

  init() {
    timer = NSTimer(timeInterval: 600         


        
相关标签:
3条回答
  • 2020-12-11 03:24

    You've found the primary use case of the Implicitly Unwrapped Optional.

    • You need to access self from init, before timer is initialized.
    • Otherwise, timer should never be nil, so you shouldn't have to check it outside of init.

    So, you should declare let timer: NSTimer!.

    0 讨论(0)
  • 2020-12-11 03:33

    It's possible that Swift 2 has changed what works. I'm using code like the following:

    @objc class Test : NSObject {
        var timer : NSTimer!
        init text() {
            super.init()
            timer = NSTimer(timeInterval: 1.0, target: self, selector: "timerFired:", userInfo: nil, repeats: true)
            NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
        }
    }
    

    Needed both @objc declaration and NSObject subclass. Also timer needs to be a var, not let.

    0 讨论(0)
  • 2020-12-11 03:39

    Aside from the implicitly unwrapped optional, I found that to get the code working I needed to subclass NSObject and to also add the timer to current run loop.

    class UpdateManager:NSObject {
        let timer: NSTimer!
    
        override init() {
            super.init()
            timer = NSTimer(timeInterval: 600, target: self, selector: "check", userInfo: nil, repeats: true)
            NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
        }
    
    
        func check() {
            // Do some stuff
    
        }
    }
    

    Updated code based on comments - thank you to jtbandes and Caroline

    class UpdateManager {
        let timer: NSTimer!
    
       init() {
    
            timer = NSTimer.scheduledTimerWithTimeInterval(600,
                target: self,
                selector: "check",
                userInfo: nil,
                repeats: true)
        }
    
    
       @objc func check() {
            // Do some stuff
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题