Accessing "self" in initializing closure

ساخت وبلاگ

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

back soft...
ما را در سایت back soft دنبال می کنید

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