[Swift] DAY14: Auto Reference count

Source: Internet
Author: User

Auto reference count

First to accompany her sister to see the film, Dot code to occupy a hole.

Weak references

It can be preceded by weak an indication that this is a weak reference. A weak reference does not hold the referenced instance, and it does not prevent the ARC from destroying the referenced instance.

You can see how to avoid circular references by using weak references in the following example:

classperson { LetNameString    varApartment:apartment? Init (Name:String{self.name = name} deinit {println ("\ (name) is being deinitialized")    }}classApartment { LetNumber:int weakvarTenant:person? Init (number:int) {self.number = number} deinit {println ("Apartment #\ (number) is being deinitialized")    }}varWhy:person?varnumber604:apartment?why = person (name:"Why") number604 = Apartment (number:604) why!. Apartment = number604number604!. Tenant = WHYPRINTLN ("Nil 1") Why = Nilprintln ("Nil 2") number604 = Nil
No master reference

It can be preceded by unowned an indication that this is a no-master reference. Similar to weak references, a no-master reference does not hold the referenced instance firmly. Unlike a weak reference, a non-primary reference is always valued. Therefore, no primary reference is always defined as a non-optional type.

The following example:

classCustomer { LetNameString    varCard:creditcard? Init (Name:String{self.name = name} deinit {println ("\ (name) is being deinitialized")    }}classCreditCard { LetNumber:int unowned LetCustomer:customer Init (Number:int, customer:customer) {self.number = number Self.customer = Customer } deinit {println ("Card #\ (number) is being deinitialized")    }}varJohn:customer?john = Customer (name:"Why") john!. Card = CreditCard (number:1234_5678_9012_3456, customer:john!) John = Nil
References
    • Automatic Reference Counting

[Swift] DAY14: Auto Reference count

Related Article

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.