Learn more about Automatic Reference Counting (ARC) in Xcode 4.2)

Source: Internet
Author: User

UnderstandingXcode 4.2Automatic Reference CountingARC) Is the content to be explained in this article, Automatic Reference Counting (ARC), Automatic reference counting, is a compilation-level feature when developing a Cocoa program, used for automatic memory management.

InXCode 4.2.ARCFeatures.

If your iOS SDK is iOS5 seed release 2, you need to make the following changes to avoid compilation errors:

 
 
  1. In
  2. System/Library/Frameworks/CoreFoundation. framework/Headers/CFBase. h
  3. To:
  4. CFTypeRef CFMakeCollectable (CFTypeRef cf) CF_AUTOMATED_REFCOUNT_UNAVAILABLE;

Modify:

 
 
  1. CFTypeRef CFMakeCollectable (CFTypeRef cf );
  2.  
  3. In System/Library/Frameworks/Foundation. framework/Hea ders/NSObject. h, set:
  4.  
  5. Return (_ bridge_retain CFTypeRef) X;

Modify:

 
 
  1. return (__bridge_retained CFTypeRef)X;  

Using ARC will keep you away from annoying and easy-to-Miss operations such as retain, release, and autorelease.

The working principle of ARC is to automatically add the code retain, release, or autorelease for memory operations to the desired position when you compile the program. That is, the same memory management mechanism as Manual Reference Counting is used at the underlying layer. However, XCode automatically adds code for memory operations during compilation, which simplifies programming.

-Fobjc-ARC must be added to the compilation option when arc is enabled. However, this is done by XCode when creating a project template.

In versions earlier than XCode 4.2, ARC is not supported.

There are also requirements for the operating system: Mac OS X v10.6 or v10.7 (64-bit applications), iOS4 or iOS5. Note: Mac OS X v10.6 and iOS4 do not support weak references (weak references: What is weak references ).

Xcode 4.2 provides a tool named "Convert to Objective-C Automatic Reference Counting". Under the Edit-> Convert menu, you can automatically convert the old code using Manual Reference Counting into new code using ARC, for example, removing the call to retain and release ).

An example of code using ARC:

 
 
  1. @interface Person : NSObject   
  2. @property (nonatomic, strong) NSString *firstName;   
  3. @property (nonatomic, strong) NSString *lastName;   
  4. @property (nonatomic, strong) NSNumber *yearOfBirth;   
  5. @property (nonatomic, strong) Person *spouse;   
  6. @end   
  7. @implementation Person   
  8. @synthesize firstName, lastName, yearOfBirth, spouse; @end  

Note: you do not need to reload the dealloc function because there is no release operation. The meaning of strong will be introduced later .)

Example 2:

 
 
  1. (void)contrived { Person *aPerson = [[Person alloc] init];   
  2. [aPerson setFirstName:@"William"];   
  3. [aPerson setLastName:@"Dudney"];  
  4.  [aPerson:setYearOfBirth:[[NSNumber alloc] initWithInteger:2011]];  
  5.  NSLog(@"aPerson: %@", aPerson);   
  6. }  

Note: The release operation is unavailable.

Example 3: (void) takeLastNameFrom :( Person *) person {NSString * oldLastname = [self lastName]; [self setLastName: [person lastName]; NSLog (@ "Lastname changed from % @ to % @", oldLastname, [self lastName]);} Note: ARC ensures that the object referenced by oldLastname is before NSLog execution ends, will not be released.
Using ARC rules: you cannot directly call the dealloc method, or directly call methods such as retain, release, retainCount, or autorelease.

But it can be called in the form of @ selector (retain) and @ selector (release.

The custom dealloc method cannot call [super dealloc]. The compiler will automatically add this code for you.

You can still use CFRetain, CFRelease, and other methods for Core Foundation-style objects. You cannot use NSAllocateObject or NSDeallocateObject to create an object. Use the alloc method. Object pointers cannot be used in struct in C language. Discard the C struct and use the Objective-C class. There is no implicit type conversion between id and void *. Use explicit type conversion. You cannot use the NSAID utoreleasepool. ARC provides the @ autoreleasepool statement block.

For example:

 
 
  1. @autoreleasepool {   
  2. // Code, such as a loop that creates a large number of temporary objects.   
  3. }  

NSZone cannot be used. The names of methods and variables cannot start with "new. About the object lifecycle: weak reference: setting it to weak does not affect the object lifecycle. If the referenced object has been released, the reference will point to nil. Strong reference: if it is set to a strong attribute, the lifecycle of the object will be affected. For example:

 
 
  1. @ Property (strong) MyClass * myObject; is equivalent to @ property (retain) MyClass * myObject.

For example:

 
 
  1. @ Property (weak) MyClass * myObject; and @ property (assign) MyClass * myObject;

In most cases, it is equivalent, but when the instance is released, the reference set to weak will point to nil.

Available restrictions: _ strong, default _ weak _ unsafe_unretained. The difference between this parameter and weak is that when an object is released, the reference does not point to nil. _ Autoreleasing: when the method parameter is id * and you want the object to be autoreleased when the method returns, you can add the _ autoreleasing qualifier. Be careful when using _ weak, such

 
 
  1. NSString _ weak * string = [[NSString alloc] initWithFormat: @ "First Name: % @", [self firstName];
  2. NSLog (@ "string: % @", string); // string is empty at this time, because the weak type does not affect the object lifecycle, and the object is released upon creation.

Other features: variables defined by strong, weak, and autoreleasing are implicitly initialized to nil.

For example:

 
 
  1. -(Void) myMethod {NSString * name; NSLog (@ "name: % @", name); // outputs null
  2. }

You are welcome to add and find bugs.

Summary: UnderstandingXcode 4.2Automatic Reference CountingARC!

Address: http://blog.sina.com.cn/s/blog_4c4c79950100t3uy.html

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.