IOS review notes 9: ARC function, ios review notes arc

Source: Internet
Author: User

IOS review notes 9: ARC function, ios review notes arc
1. Introduction to ARC
ARC is a function of the editor.
It will automatically release the alloc objects.
It automatically generates the release in dealloc for variables using the retain attribute;

When you select the ARC function when creating a project, you cannot call it:
Retain
RetainCount
Release
Autorelease
[Super dealloc]


Second ARC judgment criteria
1 criterion: as long as there is no strong pointer object, the object will be destroyed.


2 pointers are divided into two types:
1 strong pointer: _ strong. By default, all pointers are strong pointers.
2. weak pointer: _ weak. If the weak Pointer Points to an object that does not exist, it will be cleared.


Example of code with three strong pointers:

Person * p = [[Person] init]; p = nil; // clear the pointer. If no strong pointer points to the memory object, the object will be released.
Or
P = [[Person] init]; // The Pointer Points to another object. The previously allocated memory object is released.

Person * person1 = [[Person] init]; Person * person2 = person1; person1 = nil; // destroyed after the function is executed

Example of weak pointer code:
Person * person1 = [[Person] init] ;__ weak Person person2 = p; person1 = nil; // destroy the object


Note the following incorrect syntax:
_ Weak Person * person1 = [[Person] init]; // There is a warning that the created object will be immediately released


Four property Parameters
1 strong: used for OC objects. It is a "strong" pointer for member variables.
Strong replaces retain and does not need to be release in dealloc. The function is the same as retain.
For example:
@ Property (nonatomic, strong) Book * book;


2 weak: used for OC objects. The "weak" pointer in member variables
Strong replaces assign. When the book points to the object for destruction, it is automatically cleared.
@ Property (nonatomic, weak) Book * book;


3 assign: used for basic data types


V. ARC Conversion Function of Xcode
1 Project to ARC
Edit-> Refactor-> Convert to Object-c arc, check, next;
A preview is displayed. We can see that release and autorelease are deleted,
The retain of property becomes strong.
Click Save and Enable.



2 compatible with non-ARC
Click project-> Build Phases: Complie Source to view the Source files in the project,
Press enter, and enter:
-Fno-objc-arc
In this way, you can use non-ARC-related items in this file.


3. ARC compatibility with non-ARC Projects
In 2, enter:
-F-objc-arc
In this way, the ARC files can be compatible in non-ARC projects.


Circular references in section 6 ARC
Mutual objects, set the property with a short life cycle to weak

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.