Automatic Reference Counting (ARC)是编译器自动管理Objective-C对象的一个功能,相对于不得不考虑retain和release操做来讲,ARC让咱们有更多的精力集中在咱们应用内有趣的代码、object graphs和对象之间的关系上。安全
ARC是用过来在编译的时候添加适当的代码来保证对象在有用的时候有效,没有了就再也不有效了。从概念上讲,ARC经过调用合适的内存管理方法遵循着和 manual reference counting(MRC)一样的内存管理规则。app
为了让编译器产生正确的代码,ARC严格规定了你能够调用的方法和怎样使用toll-free bridging。ARC引入了新的用于对象引用和属性声明的生命周期标识符。函数
ARC支持Xcode 4.2 for OS X v10.6 and v10.7 (64-bit applications) and for iOS 4 and iOS 5。弱引用不支持OS X v10.6 and iOS 4。工具
Xcode提供了一个工具用来把非ARC代码自动转换到ARC代码(例如移除retain和release的调用),解决的不能自动迁移的问题。这个工具会把工程中的全部文件都转换成ARC,在使用非ARC方便的状况下,你也能够选择某些文件使用ARC。oop
不用再记住何时该使用retain、release和autorelease了,ARC知道你的对象的使用周期而后在编译的时候加入了合适的内存管理代码。编译器也会为你生成合适的dealloc方法。若是你刚刚使用ARC,那么传统的Cocoa的命名规范在你管理MRC的代码的时候是很是重要的。性能
一个完整的正确的Person类的实现是这样的:spa
@interface Person : NSObject @property NSString *firstName; @property NSString *lastName; @property NSNumber *yearOfBirth; @property Person *spouse; @end @implementation Person @end
对象的属性默认是strong的。指针
使用ARC,你可能实现这样一个方法contrived :code
- (void)contrived { Person *aPerson = [[Person alloc] init]; [aPerson setFirstName:@"William"]; [aPerson setLastName:@"Dudney"]; [aPerson setYearOfBirth:[[NSNumber alloc] initWithInteger:2011]]; NSLog(@"aPerson: %@", aPerson); }
ARC接管里内存管理,因此Person和NSNumber都不会形成内存泄露。orm
你也能够安全的实现一个Person的方法takeLastNameFrom:
- (void)takeLastNameFrom:(Person *)person { NSString *oldLastname = [self lastName]; [self setLastName:[person lastName]]; NSLog(@"Lastname changed from %@ to %@", oldLastname, [self lastName]); }
ARC可以确保 oldLastName
在NSLog以前不被释放。
ARC提出了一些新的别的编译器没有的新规则。这些规定是为了可以提供一个完整的可靠的内存管理模型。在一些状况可以很简单的提高性能,在一些状况下可以简化代码或者让你不用处理内存管理。若是你违背这些规则,在编译的时候就会出错,而不是在运行的时候产生错误。
@selector(retain)
,
@selector(release)等等
NSAllocateObject 和
NSDeallocateObject
// Won't work: @property NSString *newTitle; // Works: @property (getter=theNewTitle) NSString *newTitle;
// The following declaration is a synonym for: @property(retain) MyClass *myObject; @property(strong) MyClass *myObject; // The following declaration is similar to "@property(assign) MyClass *myObject;" // except that if the MyClass instance is deallocated, // the property value is set to nil instead of remaining as a dangling pointer. @property(weak) MyClass *myObject;在ARC下,strong是默认的对象类型。
__strong __weak __unsafe_unretained __autoreleasing
ClassName * qualifier variableName;例如:
MyClass * __weak myWeakReference; MyClass * __unsafe_unretained myUnsafeReference;其余变种写法在技术上是不正确的,当时是被编译器容许,想要了解更过能够看这里: http://cdecl.org/
NSString * __weak string = [[NSString alloc] initWithFormat:@"First Name: %@", [self firstName]]; NSLog(@"string: %@", string);虽然string是在初始化赋值以后使用的,可是在赋值的时候没有强引用指向string,所以它马上就会被释放。NSLog语句会输出一个null值(在此例子中编译器会给出警告)。
NSError *error; BOOL OK = [myObject performOperationWithError:&error]; if (!OK) { // Report the error. // ...然而这个声明是错误的并且是隐式的。
NSError * __strong e;其实这个方法的声明以下:
-(BOOL)performOperationWithError:(NSError * __autoreleasing *)error;
NSError * __strong error; NSError * __autoreleasing tmp = error; BOOL OK = [myObject performOperationWithError:&tmp]; error = tmp; if (!OK) { // Report the error. // ...
MyViewController *myController = [[MyViewController alloc] init…]; // ... myController.completionHandler = ^(NSInteger result) { [myController dismissViewControllerAnimated:YES completion:nil]; }; [self presentViewController:myController animated:YES completion:^{ [myController release]; }];正像是描述的同样,你可使用__block标识符,在completion handler内设置myController为nil:
MyViewController * __block myController = [[MyViewController alloc] init…]; // ... myController.completionHandler = ^(NSInteger result) { [myController dismissViewControllerAnimated:YES completion:nil]; myController = nil; };另外,你可使用一个临时的__weak变量。下面的例子声明了一个简单是实现:
MyViewController *myController = [[MyViewController alloc] init…]; // ... MyViewController * __weak weakMyViewController = myController; myController.completionHandler = ^(NSInteger result) { [weakMyViewController dismissViewControllerAnimated:YES completion:nil]; };没有了循环引用,可是你应该这样用:
MyViewController *myController = [[MyViewController alloc] init…]; // ... MyViewController * __weak weakMyController = myController; myController.completionHandler = ^(NSInteger result) { MyViewController *strongMyController = weakMyController; if (strongMyController) { // ... [strongMyController dismissViewControllerAnimated:YES completion:nil]; // ... } else { // Probably nothing... } };在一些状况下若是__weak是不兼容的你可能使用__unsafe__unretained。然而对于循环引用这个可能变得不切实际,由于是很是可贵或者不可能验证__unsafe__unretained指针是有效的和指向了一样有问题的对象。
@autoreleasepool { // Code, such as a loop that creates a large number of temporary objects. }这个简单的结构能够容许编译器来推断引用计数状态。在入口处,一个autorelease pool被放进栈。在退出的地方(break,return,goto,fall-through等等)autorelease pool被弹出栈。为了兼容已经存在的代码,若是因为异常退出,autorelease pool是不会被弹出栈的。
- (void)myMethod { NSString *name; NSLog(@"name: %@", name); }NSLog语句将会输出null而不是crashing。