using weak/unowned self in property closure when there is an init function

ref – http://stackoverflow.com/questions/39560860/uiview-cmdevicemotionhandler-unowned-may-only-be-applied-to-class-and-class-b

self_in_closure_without_init

The line of code you’re on is actually run during the init function. self is not available until after all stored properties are initialized. You’re in the middle of the process.

The closure businessCardName we’re defining is in the middle of the init process, and thus, do not exist yet.

Solution:

Declare it indeed as lazy var instead of let, so the code is not run during init but in fact at first use.

Closure Solution

used like this:

its a closure, so to use it, we have to use the parentheses.

Using Anonymous Functions (also closures)

You can also use anonymous functions, execute it by using “()”, then assign the result back to variable.

used like this: