[Swift] Day15: Circular strong reference in the closure

Source: Internet
Author: User

[Swift] Day15: Circular strong reference in the closure
Circular strong references in closures

You can define the capture list to solve the circular strong reference between closures and class instances.

Capture list

Each element in the capture list is composed of the weak or unowned keyword and instance reference (such as self. Each pair is in square brackets separated by commas:

lazy var someClosure: (Int, String) -> String = {    [unowned self] (index: Int, stringToProcess: String) -> String in}

We need to determine whether the attributes in the capture list are weak references or no primary references. The following judgment bases are available:

  • When the closure and the captured instance are always referenced and destroyed at the same time, the capture in the closure is defined as a non-master reference.
  • When capturing a reference, it may benilIs defined as weak reference. Weak references are always optional. When the referenced instance is destroyed, the value of the weak reference is automatically setnil. This allows us to check whether they exist in the closure.

    For example, the following code:

    class HTMLElement {    let name: String    let text: String?    lazy var asHTML: () -> String = {        [unowned self] in        if let text = self.text {            return "<\(self.name)>\(text)
       "        } else {            return "<\(self.name) />"        }    }    init(name: String, text: String? = nil) {        self.name = name        self.text = text    }    deinit {        println("\(name) is being deinitialized")    }}var paragraph: HTMLElement? = HTMLElement(name: "p", text: "hello, world")println(paragraph!.asHTML())

    Actually definedweakIt is also possible (right), but each time you need to unpack, but in factselfImpossiblenilSo such a definition is meaningless. Can be better expressed with no primary referenceself.

    Summary

    The basic part of the official document has finally been read, and the time is limited. Many details have not been learned yet, so you can only make up the following.

    Next we plan to look at some open-source projects and learn more about using Swift by reading other people's source code.

    Come on ~

    (Ah, I'm sorry for being too watery recently. I just copied the example on the official website! No. You have to work hard tomorrow !)

    References
    • Automatic Reference Counting

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.