Mechanism and usage of ARC in ios

Source: Internet
Author: User

[Html]

 

What is ARC?

ARC is a new feature launched by iOS 5, which is called Automation Reference Counting ). Simply put, retain/release is automatically added to the Code. The Code that was manually added to handle the reference count of memory management can be automatically completed by the compiler.

This function starts to be imported in iOS 5/Mac OS X 10.7 and can be used in Xcode4.2. A simple understanding of ARC is to allow the compiler (LLVM 3.0) to automatically generate the reference count of instances to manage part of the code During code compilation through the specified syntax. One thing is that ARC is not a GC, but a Static Analyzer tool.

Change Point
Through a short piece of code, we can see the changes before and after using ARC.

[Html]
@ Interface NonARCObject: NSObject {
NSString * name;
}
-(Id) initWithName :( NSString *) name;
@ End
@ Implementation NonARCObject
-(Id) initWithName :( NSString *) newName {
Self = [super init];
If (self ){
Name = [newName retain];
}
Return self;
}
-(Void) dealloc {
[Name release];
[Super dealloc];
}
@ End

@ Interface NonARCObject: NSObject {
NSString * name;
}
-(Id) initWithName :( NSString *) name;
@ End
@ Implementation NonARCObject
-(Id) initWithName :( NSString *) newName {
Self = [super init];
If (self ){
Name = [newName retain];
}
Return self;
}
-(Void) dealloc {
[Name release];
[Super dealloc];
}
@ End
[Html]
@ Interface ARCObject: NSObject {
NSString * name;
}
-(Id) initWithName :( NSString *) name;
@ End
@ Implementation ARCObject
-(Id) initWithName :( NSString *) newName {
Self = [super init];
If (self ){
Name = newName;
}
Return self;
}
@ End

@ Interface ARCObject: NSObject {
NSString * name;
}
-(Id) initWithName :( NSString *) name;
@ End
@ Implementation ARCObject
-(Id) initWithName :( NSString *) newName {
Self = [super init];
If (self ){
Name = newName;
}
Return self;
}
@ End

When we used Objective-C memory management rules, we often used the following guidelines:
When generating an object, use autorelease
When the object is replaced, autorelease is used before retain
When an object is returned in a function, use return [[object retain] autorelease];
After using ARC, we don't need to do this, even the most basic release.

Benefits of using ARC
What are the advantages of using ARC?

As you can see from the above example, it will be much easier to write Objective-C code in the future, because we don't need to worry about annoying memory management and worry about memory leakage.
The total amount of code is reduced, and it seems a little refreshing, saving labor.
High-speed code. The use of compilers to manage reference counts reduces the possibility of inefficient code.
Poor
Remember a bunch of new ARC rules-certain learning cycles are required for keywords and features.
Some old code is troublesome for third-party code. to modify the code, you must manually modify the compilation switch.

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.