[swift-users] Initializer accessing self

Brandon Knope bknope at me.com
Wed Feb 1 22:06:02 CST 2017


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) {
        
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170201/f36d8ade/attachment.html>


More information about the swift-users mailing list