خرید بک لینک

Vote count: 0

In Swift 3 the dispatch_once function was removed and the migration guide suggests to use initializing closure:

let myGlobal = { … global contains initialization in a call to a closure … }()

_ = myGlobal // using myGlobal will invoke the initialization code only the first time it is used.

I'd like to access 'self' instance variables from within the initializing closure like so:

class SomeClass {
    var other = SomeOtherClass()

    let initialize: () = {
        // self.other - this doesn't work, complains about unresolved identifier 'self'
        // how to access self.other here?
    } ()

    func doSomething() {
        // initialize will only be called once
        initialize
    }
}

Why is 'self' not accessible in the closure and how can make it to be?

asked 1 min ago

برچسب: accessing self in block, نویسنده: استخدام کار تاريخ: يکشنبه 28 شهريور 1395 ساعت: 6:15

صفحه بندی