autorelease 释放池
1>autorelease基本使用法:
*首先把一个对象放到池子中
*当我们自动释放池被销毁时,会对里面的对象做一次release操作
*返回对象本身
*调用完autorelease后,对象的计数器是不变,不会对它减一
*如果调用了两次autorelease,会报错,野指针
*/
1 #import <Foundation/Foundation.h> 2 #import "Person.h" 3 int main(int argc, const char * argv[]) { 4 // Person *p = [[Person alloc]init]; 5 // [p release]; 6 // 7 @autoreleasepool { // 代表创建释放池 8 Person *p1 = [[[Person alloc]init]autorelease]; 9 // [p1 release]; 10 p1.age = 10; 11 12 @autoreleasepool { 13 // 错误的(面试题) 14 Person *p2 = [[[[Person alloc]init]autorelease]autorelease]; 15 16 } 17 18 19 } // 结束代表销毁释放池 20 return 0; 21 }
1 #import <Foundation/Foundation.h> 2 3 @interface Person : NSObject 4 @property(nonatomic,assign)int age; 5 @end
1 #import "Person.h" 2 3 @implementation Person 4 5 6 - (void)dealloc { 7 8 NSLog(@"person对象被销毁"); 9 [super dealloc]; 10 } 11 @end