Global macro definition in OC
#define WS (weakself) __weak __typeof (&*self) weakself = self;
Use the following:
WS (weakself)
[Self.tableview addheaderwithcallback:^{
[Weakself requestmemberlist];
}];
Swift uses weakself in a closed packet
weak var weakself = self
Demo4 {
The advantage of using is that once self is released, nothing is done.
Weakself?. View.backgroundcolor =uicolor.redcolor ()
}
This is the Advanced method:
A class in Swift can be nested to define another class, and the newly added class will only be used by the current class
In Swift, to dismiss a circular reference to a closure, you can use [unowned self] or [weak self] in the closure definition, where:
[unowned Self] is similar to unsafe_unretained in OC, if the object is freed, still retains an invalid reference, not an option
[Weak self] similar to the __weak in OC, if the object is released, automatically points to nil, more secure
[weakself] always monitor poor performance, [unowned self] may cause wild pointer errors, if you can determine that the object will not be released, try to use unowned
Example lazy-loaded closures
Lazy var printname: ()--() = {[unowned self] in
Print (Self.name)
}
The following sentence does not occur because a circular reference will disappear after execution
Lazy var title:string = {
Print (Self.name)
Return "Mr. \ (self.name)"
}()
The block in Swift's closure oc resolves the circular reference