Initializing Swift properties that require “self” as an argument

后端 未结 3 669
一向
一向 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: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.

提交回复
热议问题