Swift Learning Notes (vii) Self-active reference counting

Source: Internet
Author: User

Like Object-c, Swift uses its own active reference count to track and manage the memory used by the app. When the instance is no longer being used, and retaincount=0, it will voluntarily release the memory space occupied by the rationale.

Note: Reference counts apply only to instances of classes, and because structs and enumeration are value types, they do not involve references, so they are stored and managed in a way that is not a reference count.

When an instance is initialized, the system will voluntarily allocate a certain amount of memory space for managing properties and methods. When an instance object is no longer being used, its memory space is retracted.

There are three kinds of reference types in Swift, that is, strong strong references, weak weak references, and no master references unwired

A strong reference guarantees that the object will not be freed during use. But the same time also led to the emergence of a strong citation ring.

A strong reference ring is a case where two class instances keep each other's strong references, and two instances are referenced by each other and cannot be released.

To solve the problem of a strong reference ring, you can use a weak reference or no master reference to implement.

Use weak weak references: For life cycles, references can become nil instances, use weak references, and add keywordweak before the declaration;

When using a non-primary reference: For the initialization assignment, the reference is no longer assigned to nil, using a no-primary reference and using keywordunowned when declaring.

Attention:

1. Weak: Because weakly referenced properties, in the life cycle, may be referred to as nil, so must be declared as a variable type, cannot be a constant type.

And because a weak reference might not have a value, declare it as an optional type, "? "For example: weak var name:string?"

2.unowned: Because no primary type is assigned, it cannot be empty, so it is declared as a non-selectable type. At the same time, when using a no-master reference, there is no need to expand, direct access to ask.

A non-optional type variable cannot be assigned a value of nil, so it cannot be set to nil when the instance is destroyed.

3.

When an instance is destroyed, an attempt to access a non-primary reference to that instance triggers an execution-time error. When using a no-master reference, make sure that the reference always points to an instance that is not destroyed. The illegal operation above will cause the application to crash, and will not cause unexpected behavior. Therefore, you should avoid such a situation.



Swift Learning Notes (vii) Self-active 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.