Automatic Reference Counting
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Automatic Reference Counting相关的知识,希望对你有一定的参考价值。
- (id)allocObject { id obj = [[NSObject alloc] init]; return obj; } - (id)object { id obj = [[NSObject alloc] init]; [obj autorelease]; return obj; } id obj1 = [obj0 allocObject]; id obj2 = [obj0 object];
obj1持有对象,obj2不持有对象。
通过使用autorelease,可以使取得的对象存在,但自己不持有对象。(如何做到的?)
[obj2 retain];
通过retain方法将调用autorelease方法取得的对象变为自己持有。(retain多次会怎样?)
NSObject简化版alloc:
struct obj_layout { NSUInteger retained; }; + (id)alloc { int size = sizeof(struct obj_layout) + 对象大小; struct obj_layout *p = (struct obj_layout *)calloc(1, size); return (id)(p + 1); }
以上是关于Automatic Reference Counting的主要内容,如果未能解决你的问题,请参考以下文章
Swift-自动引用计数(Automatic Reference Counting)(十四)
长路漫漫,唯剑作伴--Automatic Reference Counting
xcode禁用ARC(Automatic Reference Counting)
Swift-自动引用计数(Automatic Reference Counting)(十四)
does not support Objective-C Automatic Reference Counting (ARC)
JSONKit does not support Objective-C Automatic Reference Counting(ARC) / ARC forbids Objective-