码迷,mamicode.com
首页 > 其他好文 > 详细

OC11_自动释放池

时间:2015-06-18 21:47:57      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

//
//  Dog.h
//  OC11_自动释放池
//
//  Created by zhangxueming on 15/6/18.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Dog : NSObject

@property (assign, nonatomic)NSInteger age;

@end



//
//  Dog.m
//  OC11_自动释放池
//
//  Created by zhangxueming on 15/6/18.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "Dog.h"

@implementation Dog

- (void)dealloc
{
    NSLog(@"dog is release");
    [super dealloc];
}

@end
//
//  main.m
//  OC11_自动释放池
//
//  Created by zhangxueming on 15/6/18.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Dog.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //自动释放是就是一个数组,自动释放池会将池中所有的对象都release一次
        Dog *xiaoHei = [[Dog alloc] init];
        xiaoHei.age = 12;
        NSLog(@"retainCount = %li", xiaoHei.retainCount);
        
        [xiaoHei autorelease];//将xiaoHei 的拥有权交给了自动释放池
        //[array addobject: xiaoHei];//2
        //[xiaoHei release];//1
        
        NSLog(@"retainCount = %li", xiaoHei.retainCount);//正确
    
        Dog *xiaoBai = [[Dog alloc] init];
        [xiaoBai autorelease];
        
    }
    
    //可以有多个释放池
    @autoreleasepool {
        
        
        
        
    }
    
    
    return 0;
}

 

OC11_自动释放池

标签:

原文地址:http://www.cnblogs.com/0515offer/p/4586976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!