The block in Swift's closure oc resolves the circular reference

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.