[objective-c] Transitions from reference count to Automatic reference count (ARC)

Source: Internet
Author: User


Turn from: https://developer.apple.com/library/mac/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html






transitioning to ARC release Notes






Automatic Reference Counting (ARC) is a compiler feature this provides Automatic memory management of OBJECTIVE-C. Rather than has to is about retain and release operations, ARC allows you to concentrate on the interesting code, T He object graphs, and the relationships between objects in your application.





Summary



ARC works by adding code at compile time to ensure that objects live as long as necessary, but no longer. Conceptually, it follows the same memory management Conventions as manual reference counting (described in Advanced memory Management programming Guide) by adding the appropriate memory to Management for you.



In order for the compiler to generate correct code, ARC restricts the methods can use and how do you use toll-free Bridgi Ng (Toll-Free bridged Types). ARC also introduces new lifetime qualifiers for object references and declared properties.



ARC is supported in Xcode 4.2 for OS X v10.6 and v10.7 (64-bit applications) and for iOS 4 and iOS 5. Weak references are not supported in OS X v10.6 and IOS 4.



Xcode provides a tool that automates mechanical parts to the ARC conversion (such as removing retain and release calls ) and helps to fix issues the Migrator can ' t handle automatically (choose Edit > Refactor > Convert to Objective -C ARC). The Migration tool converts all files in a project to use ARC. can also choose to use ARC on a per-file basis if it's more convenient for your to use manual reference counting Me files.



ALSO:



Advanced Memory Management Programming Guide



Memory Management programming Guide for Core Foundation ARC Overview



Instead of you have to remember when to use retain, release, and Autorelease, ARC evaluates the lifetime requirements of Your objects and automatically inserts appropriate memory management calls for your at compile time. The compiler also generates appropriate dealloc for you. In general, if you are only using ARC the traditional Cocoa naming conventions are important only if you need to Interopera Te with the code that uses manual reference counting.



A complete and correct implementation of a person class might look like this:


@interface Person:nsobject
@property NSString *firstname;
@property NSString *lastname;
@property NSNumber *yearofbirth;
@property person *spouse;
@end
@implementation person
@end


(Object properties are strong by default, the strong attribute is described in ARC introduces New Lifetime.)



Using ARC, you could implement a contrived as this:


-(void) contrived {
    Person *aperson = [[Person alloc] init];
    [Aperson setfirstname:@ "William"];
    [Aperson setlastname:@ "Dudney"];
    [Aperson Setyearofbirth:[[nsnumber alloc] initwithinteger:2011]];
    NSLog (@ "Aperson:%@", Aperson);
}


ARC takes care of memory management so this neither the person nor the NSNumber objects.



You are could also safely implement a takelastnamefrom:method of person as this:


-(void) Takelastnamefrom: (person *) person {
    NSString *oldlastname = [self lastName];
    [Self Setlastname:[person lastName]];
    NSLog (@ "Lastname changed from%@ to%@", Oldlastname, [self Lastname]);
}


ARC ensures that oldlastname are not deallocated before the NSLOG statement. ARC enforces New Rules



To work, ARC imposes some the new rules that are not present when using the other compiler modes. The rules are intended to provide a fully reliable memory model; In some cases, they simply enforce best practice, in some others they simplify code or your are obvious R not has to deal with memory management. If you violate this rules, you are immediate compile-time error, not a subtle bug this may become apparent at runtime.



You are cannot explicitly invoke Dealloc, or implement or invoke retain, release, Retaincount, or autorelease.



The prohibition extends to using @selector (retain), @selector (release), and.



You could implement a Dealloc method if you need to manage resources other than releasing instance. You don't have to (indeed your cannot) Release instance variables, but your may need to invoke [Systemclassinstance Setdele Gate:nil] on system classes and other code that isn ' t compiled using ARC.



Custom Dealloc methods in ARC does not require a call to [Super Dealloc] (it actually results in a compiler Error). The chaining to super are automated and enforced by the compiler.



Can still use Cfretain, Cfrelease, and the related functions with Core Foundation-style objects (* Managing Toll-f REE bridging).



You are cannot use Nsallocateobject or Nsdeallocateobject.



You create objects using Alloc; The runtime takes care of Deallocating objects.



You are cannot use object pointers in C structures.



Rather than using a struct, you can create a objective-c class to manage the data instead.



There is no casual casting between ID and void *.



You are must use special casts this tell the compiler about object lifetime. You are need to does this to cast between Objective-c objects and Core Foundation types the You pass as function arguments. For more details, managing toll-free bridging.



Cannot use NSAutoreleasePool objects.



ARC provides @autoreleasepool blocks instead. These have a advantage of being more efficient than nsautoreleasepool.



Cannot use memory zones.



There is no need to use nszone any more-they are ignored by the modern objective-c runtime.



To allow interoperation with manual retain-release code, ARC imposes a constraint on method naming:



You are cannot give a accessor a name that begins with new. This in turn means so you can ' t, for example, declare a property whose name begins with new unless a specify NT Getter:


Won ' t work:
@property NSString *newtitle;
Works:
@property (getter=thenewtitle) NSString *newtitle;


ARC introduces New Lifetime qualifiers

ARC introduces several new lifetime qualifiers for objects, and weak references. A Weak reference does not extend the lifetime of the object it points to, and automatically becomesnil when there are no s Trong references to the object.



You should take advantage of this qualifiers to manage the object graphs in your. In particular, ARC does not guard Against strong reference cycles  (previously known as retain cycles-see 


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.