[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.