OC学习笔记08--OC内存管理之autorelease和autoreleasepool

来源:互联网 发布:网络成瘾综合症 编辑:程序博客网 时间:2024/06/11 02:12
#import <Foundation/Foundation.h>// Bird@interface Bird :NSObject@property (nonatomic,assign,getter=isFlyable) BOOL flyable;- (id)initWithFlyable:(BOOL)flyable;+ (id)bird;+ (id)birdWithFlyable:(BOOL)flyabe;+ (id)birdWithFlyable2:(BOOL)flyable;@end@implementation Bird- (void)dealloc{// 利用self,可知道当前被销毁的对象是本类还是子类NSLog(@"%@ deallocated",[self className]);[super dealloc];}//构造方法- (id)initWithFlyable:(BOOL)flyable{if(self=[super init]){//使用set方法赋值方便内存管理[self setFlyable:flyable];}return self;}//提供类方法便捷创建autorelease过的对象(并非构造方法)+ (id)bird{// 灵活利用self,可以方便子类使用该方法创建子类对象return[[[self alloc] init] autorelease];}+ (id)birdWithFlyable:(BOOL)flyable{Bird *b=[self bird];[b setFlyable:flyable];return b;}// 两种类型根据参数创建autorelease过的对象+ (id)birdWithFlyable2:(BOOL)flyable{return[[[self alloc] initWithFlyable:flyable] autorelease];}@end//Penguin@interface Penguin :Bird@property (nonatomic,retain) NSString *descript;- (id)initWithFlyable:(BOOL)flyable andDescript:(NSString *)descript;+ (id)penguinWithFlyable:(BOOL)flyable andDescript:(NSString *)descript;@end@implementation Penguin- (void)dealloc{//释放retain的对象[_descript release];[super dealloc];}- (id)initWithFlyable:(BOOL)flyable andDescript:(NSString *)descript{if(self=[super initWithFlyable:flyable]){// 使用set方法进行赋值,方便内存管理;[self setDescript:descript];}return self;}// 重写输出方法- (NSString *)description{//此种方式创建的NSString对象为autorelease过的return [NSString stringWithFormat:@"flyable=%d,descript=%@",[self isFlyable],_descript];}+ (id)penguinWithFlyable:(BOOL)flyable andDescript:(NSString *)descript{Penguin *p=[super birdWithFlyable:flyable];[p setDescript:descript];return p;}@endint main(){NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];// 该方法创建的OC字符串为autorelease过NSString *descript=[NSString stringWithFormat:@"%@ cannot fly",[Penguin className]];// 调用autorelease后将对象加入栈顶池中Penguin *p1=[[[Penguin alloc] initWithFlyable:NO andDescript:descript] autorelease];// 自动释放池可嵌套,最后创建的放栈顶,将最先被销毁NSAutoreleasePool *pool2=[[NSAutoreleasePool alloc] init];Penguin *p2=[Penguin penguinWithFlyable:NO andDescript:descript];Penguin *p3=[Penguin birdWithFlyable:NO];[p3 setDescript:descript];NSLog(@"p2::%@",p2);NSLog(@"p3::%@",p3);// pool2 释放,等同于[pool2 release];[pool2 drain];NSLog(@"p1::%@",p1);[pool drain];return 0;}




autorelease 方法:
1>返回对象本身
2>将对象放到autoreleasepool中
3>当autoreleasepool销毁,对池中所有对象调用一次release

autoreleasepool两种创建方式:
1>IOS 5.0前 
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
//autorelease调用区域
[pool drain];// 或 [pool release];
2>IOS 5.0后@autoreleasepool{......}

autoreleasepool特点:
1>池子是存放在一个栈结构的对象中,嵌套创建的多个池子,先创建的池子后被销毁
2>调用autorelease方法后,将对象放入该栈顶端的池子
3>池子在{}中代码执行结束后销毁,若是IOS 5.0前,则调用[pool drain]后销毁


优点:不用关心对象release时间
缺点:对象释放不及时,影响性能,大内存对象不要使用




方法命名规范:
系统自带类中创建对象的类方法里没包含alloc、new、copy字眼的,基本返回的对象都是autorelease的,
开发中也可以使用这种方式,提供 创建autorelease过的对象 的类方法来便捷创建对象, 
这里一般使用[self alloc]创建,方便子类同样使用该方法也能创建子类对象;

类命名规范:
OC中无java 包名.类名 的方式来防止类名重复问题,使用类名加前缀的方式来避免类名重复;

0 0
原创粉丝点击