iOS ARC(Automatic Reference Count)机制

来源:互联网 发布:好书 知乎 编辑:程序博客网 时间:2024/05/17 01:54

转自:http://blog.csdn.net/xjy_860919/article/details/7315316

 

ARC 学习

iOS引入了Automatic Reference Count(ARC),编译器可以在编译时对obj-c对象进行内存管理。

之前,obj-c的内存管理方式称作引用计数,就是obj-c对象每被”使用”一次,引用计数+1,当引用计数为0时,系统会回收内存.用程序语言表达,就是allco的要release,retain/copy的要release.还有某些容器add的,也要release等等.

现如今,ARC使你勿需理会这些内存管理规则,但同时一些语法也相应的作了改变,具体有哪些变化我们一一解析.

首先新建一个工程(注意Xcode得是4.2),选择使用ARC(如果在新建工程的时候没有选择ARC,随后在些代码的时候又想使用ARC,只需在Build Setting中更改),

然后直奔main函数,

int main(int argc, char *argv[]){    @autoreleasepool {        return UIApplicationMain(argc, argv, nil, NSStringFromClass([ZJAppDelegate class]));    }} 

对比未使用ARC的main函数:

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];    int retVal = UIApplicationMain(argc, argv, nil, nil);    [pool release];    return retVal;

NSAutoreleasePool在ARC中被禁止使用,替换成@autoreleasepool 函数体.使用@ autoreleasepool,在函数入口的时候,autorelease pool入栈,正常退出时,autorelease pool出栈,从而释放变量. 注意@ autoreleasepool在非ARC模式下,也能使用,并据称使用@autoreleasepool比使用NSAutoreleasePool速度能快6倍, 明显提升程序性能.

然后打开delegate函数,会发现声明的property有个新的关键字:

@property (strong, nonatomic) UIWindow *window;

strong是新的property限定词,相当于之前的retain,还有一个新的限定词是weak相当于assign. 要注意使用weak reference的时候,当你需要使用对象的时候,可能对象已经不存在了.weak的作用通常只在于避免循环引用.

再查看任一m文件,你会发现没有dealloc函数.ARC中dealloc不需要显示实现,编译器会自己实现.有时候你设计的类会有delegate对象,这时你可以实现dealloc函数在其中将delegate置为nil,但是不要调用[super dealloc],编译器自己会调用.

此外,ARC还有变量限定词:__strong,__weak,__unsafe_unretained和__autoreleasing

__strong是默认的,__weak表明对象是zeroing weak reference,意思是当对象所指向的内存销毁了,zidong置为空nil. __unsafe_unretained是对对象的非zeroing的weak reference,意思是当对象所指向的内存被销毁了,对象还存在,成为”野指针”.__ autoreleasing用于对(id *)类型的参数传递.

Apple举了个例子,

NSString __weak *string = [[NSString alloc] initWithFormat:@"First Name: %@", [self firstName]];NSLog(@"string: %@", string);

因为在初始化的时候声明了__weak,即zeroing weak reference,初始化完后立马会被销毁,所以NSLOg时显示NULL.

因为ARC是编译时进行的,我们还可以选择哪些文件使用ARC编译,如图:

对于要自己管理内存的文件,设置值为-fno-objc-arc即可。

设置方法:When you migrate a project to use ARC, the -fobjc-arc compiler flag is set as the default for all Objective-C source files. You can disable ARC for a specific class using the -fno-objc-arc compiler flag for that class. In Xcode, in the target Build Phases tab, open the Compile Sources group to reveal the source file list. Double-click the file for which you want to set the flag, enter -fno-objc-arc in the pop-up panel, then click Done

最后,简单的总结下ARC下新规则:

  • 不能使用retai,release,retainCount,autorelease,NSAutoreleasePool. 通常情况下dealloc函数不需要实现,除非你需要置delegate为nil.
  • Strong相当于retain,weak相当于assign.
  • 声明的property中,不能以new开头.

 

原创粉丝点击