Initializer accessing self

I am at a loss:

Why does this not work:
class Test {
    let timer: Timer!
    
    init() {
        timer = Timer.scheduledTimer(timeInterval: 20, target: self, selector: #selector(test(_:)), userInfo: nil, repeats: true)
    }
    
    @objc func test(_ timer: Timer) {
        
    }
}

error: constant 'self.timer' used before being initialized
        timer = Timer.scheduledTimer(timeInterval: 20, target: self, selector: #selector(test(_:)), userInfo: nil, repeats: true)

But this does:

class TestTwo {
    var timer: Timer!
    
    init() {
        timer = Timer.scheduledTimer(timeInterval: 20, target: self, selector: #selector(test(_:)), userInfo: nil, repeats: true)
    }
    
    @objc func test(_ timer: Timer) {
        
    }
}

In the working version, I think the timer is first set to nil and then changed to the scheduled timer. In the non-working case time can only be set exactly once and self access is not legal until after it is set.

Ray

···

On Feb 1, 2017, at 8:06 PM, Brandon Knope via swift-users <swift-users@swift.org> wrote:

class Test {
    let timer: Timer!
    
    init() {
        timer = Timer.scheduledTimer(timeInterval: 20, target: self, selector: selector(test(_:)), userInfo: nil, repeats: true)
    }
    
    @objc func test(_ timer: Timer) {
        
    }
}