Sian 发表于 2014-1-11 23:11:28

第十六讲:Objective-C内存管理之“黄金法则”

Person.h//
//Person.h
//memory
//
//Created by yusian on 14-1-11.
//Copyright (c) 2014年 yusian. All rights reserved.
//

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

@interface Person : NSObject
{
    Dog * _dog;
}

- (void) setDog:(Dog *)aDog;
- (Dog *) dog;

@end
Person.m
//
//Person.m
//memory
//
//Created by yusian on 14-1-11.
//Copyright (c) 2014年 yusian. All rights reserved.
//

#import "Person.h"

@implementation Person

- (Dog *) dog{
    return _dog;
}

- (void) setDog:(Dog *)aDog{
    if (aDog != _dog) {
      ;
      _dog = ;
    }
}

- (void) dealloc{
    NSLog(@"Person is dealloc!");
    ;
    ;
}

@end
Dog.h
//
//Dog.h
//memory
//
//Created by yusian on 14-1-11.
//Copyright (c) 2014年 yusian. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Dog : NSObject
{
    int _ID;
}

@property int ID;

@end
Dog.m
//
//Dog.m
//memory
//
//Created by yusian on 14-1-11.
//Copyright (c) 2014年 yusian. All rights reserved.
//

#import "Dog.h"

@implementation Dog

@synthesize ID = _ID;

- (void) dealloc{
    NSLog(@"Dog %d is dealloc!",_ID);
    ;
}
@end
main.m
//
//main.m
//memory
//
//Created by yusian on 14-1-11.
//Copyright (c) 2014年 yusian. All rights reserved.
//

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

int main(int argc, const char * argv[])
{

    @autoreleasepool {
      
      Dog * dog1 = [ init];
      
      Dog * dog2 = [ init];
      
      Person * sim = [ init];
      
      Person * sian = [ init];
      
      ;
      
      ;
      
      NSLog(@"//初始化两条狗");
      NSLog(@"dog %d retainCount1 is %ld",,);
      NSLog(@"dog %d retainCount1 is %ld",,);
      
      ;//sim开始遛狗dog1
      
      NSLog(@"//sim开始遛狗dog1");
      NSLog(@"dog %d retainCount2 is %ld",,);
      NSLog(@"dog %d retainCount2 is %ld",,);
      
      ;//sian开始遛狗dog1
      
      NSLog(@"//sian开始遛狗dog1");
      NSLog(@"dog %d retainCount3 is %ld",,);
      NSLog(@"dog %d retainCount3 is %ld",,);
      
      ;//sim开始遛狗dog2
      
      NSLog(@"//sim开始遛狗dog2");
      NSLog(@"dog %d retainCount4 is %ld",,);
      NSLog(@"dog %d retainCount4 is %ld",,);
      
      ;//sian开始遛狗dog2
      
      NSLog(@"//sian开始遛狗dog2");
      NSLog(@"dog %d retainCount5 is %ld",,);
      NSLog(@"dog %d retainCount5 is %ld",,);
      
      ;//sim离开
      
      NSLog(@"//sim离开");
      NSLog(@"dog %d retainCount6 is %ld",,);
      NSLog(@"dog %d retainCount6 is %ld",,);
      
      ;//sian离开
      
      NSLog(@"//sian离开");
      NSLog(@"dog %d retainCount7 is %ld",,);
      NSLog(@"dog %d retainCount7 is %ld",,);
      
      NSLog(@"//dog1 and dog2 is release");
      ;
      ;
      
      
    }
    return 0;
}
输出结果:
2014-01-11 23:08:52.260 memory //初始化两条狗
2014-01-11 23:08:52.261 memory dog 1 retainCount1 is 1
2014-01-11 23:08:52.261 memory dog 2 retainCount1 is 1
2014-01-11 23:08:52.262 memory //sim开始遛狗dog1
2014-01-11 23:08:52.262 memory dog 1 retainCount2 is 2
2014-01-11 23:08:52.262 memory dog 2 retainCount2 is 1
2014-01-11 23:08:52.263 memory //sian开始遛狗dog1
2014-01-11 23:08:52.263 memory dog 1 retainCount3 is 3
2014-01-11 23:08:52.263 memory dog 2 retainCount3 is 1
2014-01-11 23:08:52.264 memory //sim开始遛狗dog2
2014-01-11 23:08:52.264 memory dog 1 retainCount4 is 2
2014-01-11 23:08:52.264 memory dog 2 retainCount4 is 2
2014-01-11 23:08:52.265 memory //sian开始遛狗dog2
2014-01-11 23:08:52.265 memory dog 1 retainCount5 is 1
2014-01-11 23:08:52.265 memory dog 2 retainCount5 is 3
2014-01-11 23:08:52.266 memory Person is dealloc!
2014-01-11 23:08:52.266 memory //sim离开
2014-01-11 23:08:52.266 memory dog 1 retainCount6 is 1
2014-01-11 23:08:52.267 memory dog 2 retainCount6 is 2
2014-01-11 23:08:52.267 memory Person is dealloc!
2014-01-11 23:08:52.267 memory //sian离开
2014-01-11 23:08:52.268 memory dog 1 retainCount7 is 1
2014-01-11 23:08:52.268 memory dog 2 retainCount7 is 1
2014-01-11 23:08:52.268 memory //dog1 and dog2 is release
2014-01-11 23:08:52.269 memory Dog 1 is dealloc!
2014-01-11 23:08:52.269 memory Dog 2 is dealloc!
Program ended with exit code: 0

页: [1]
查看完整版本: 第十六讲:Objective-C内存管理之“黄金法则”