OC10_数组的内存管理

//
//  main.m
//  OC10_数组的内存管理
//
//  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 {
        Dog *dog1 = [[Dog alloc] init];
        Dog *dog2 = [[Dog alloc] init];
        Dog *dog3 = [[Dog alloc] init];
        Dog *dog4 = [[Dog alloc] init];
        Dog *dog5 = [[Dog alloc] init];
        //往数组中添加对象, 对应对象的retainCount 值+1;
        NSLog(@"dog1 retainCount = %li", dog1.retainCount);
        NSMutableArray *dogs = [[NSMutableArray alloc] init];
        [dogs addObject:dog1];//dogs[0]-->dog1
        [dogs addObject:dog1];//dogs[1]-->dog1
        NSLog(@"dog1 retainCount = %li", dog1.retainCount);
        [dogs addObject:dog2];//dogs[2]-->dog2
        [dogs addObject:dog3];
        [dogs addObject:dog4];
        [dogs addObject:dog5];
        
        [dog1 release];
        [dog2 release];
        [dog3 release];
        [dog4 release];
        [dog5 release];
        
        NSLog(@"dog1 retainCount = %li", dog1.retainCount);
        
        //数组对象release 实际上是对数组中的每个一个元素 release 一次
        [dogs release];
        
        //NSLog(@"dog1 retainCount = %li", dog1.retainCount);
        
    }
    return 0;
}

 

posted @ 2015-06-18 20:45  sirzhang  阅读(170)  评论(0编辑  收藏  举报
AmazingCounters.com