TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
本帖最后由 Sian 于 2014-1-12 11:58 编辑
本讲是针对第十六讲黄金法则中retain与release关系的优化,却retain属性:@property (retain)
Dog.h- //
- // Dog.h
- // Memory_retain
- //
- // Created by yusian on 14-1-12.
- // 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_retain
- //
- // Created by yusian on 14-1-12.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import "Dog.h"
- @implementation Dog
- @synthesize ID = _ID;
- - (void) dealloc{
- NSLog(@"dog %d is dealloc",_ID);
- [super dealloc];
- }
- @end
复制代码 Person.h- //
- // Person.h
- // Memory_retain
- //
- // Created by yusian on 14-1-12.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "Dog.h"
- @interface Person : NSObject
- @property (retain) Dog * dog;
- @end
复制代码 Person.m- //
- // Person.m
- // Memory_retain
- //
- // Created by yusian on 14-1-12.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import "Person.h"
- @implementation Person
- @synthesize dog = _dog;
- - (void) dealloc{
- self.dog = nil;
- NSLog(@"Person is dealloc");
- [super dealloc];
- }
- @end
复制代码 main.m- //
- // main.m
- // Memory_retain
- //
- // Created by yusian on 14-1-12.
- // 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 = [[Dog alloc] init];
- Dog * dog2 = [[Dog alloc] init];
- Person * sim = [[Person alloc] init];
- Person * sian = [[Person alloc] init];
-
- [dog1 setID:1];
- [dog2 setID:2];
- NSLog(@"//初始化后的retainCount值");
- NSLog(@"dog %d retainCount1 is %ld",[dog1 ID],[dog1 retainCount]);
- NSLog(@"dog %d retainCount1 is %ld",[dog2 ID],[dog2 retainCount]);
-
- [sim setDog:dog1];
- NSLog(@"//sim开始遛狗dog1");
- NSLog(@"dog %d retainCount2 is %ld",[dog1 ID],[dog1 retainCount]);
- NSLog(@"dog %d retainCount2 is %ld",[dog2 ID],[dog2 retainCount]);
- [sian setDog:dog2];
- NSLog(@"//sian开始遛狗dog2");
- NSLog(@"dog %d retainCount3 is %ld",[dog1 ID],[dog1 retainCount]);
- NSLog(@"dog %d retainCount3 is %ld",[dog2 ID],[dog2 retainCount]);
-
- [sim setDog:dog2];
- [sian setDog:dog1];
- NSLog(@"//sim和sian交换遛狗");
- NSLog(@"dog %d retainCount4 is %ld",[dog1 ID],[dog1 retainCount]);
- NSLog(@"dog %d retainCount4 is %ld",[dog2 ID],[dog2 retainCount]);
-
- [sim setDog:dog1];
- NSLog(@"//sim和sian都转为遛狗dog1");
- NSLog(@"dog %d retainCount5 is %ld",[dog1 ID],[dog1 retainCount]);
- NSLog(@"dog %d retainCount5 is %ld",[dog2 ID],[dog2 retainCount]);
-
- [sim setDog:dog2];
- [sian setDog:dog2];
- NSLog(@"//sim和sian都转为遛狗dog2");
- NSLog(@"dog %d retainCount6 is %ld",[dog1 ID],[dog1 retainCount]);
- NSLog(@"dog %d retainCount6 is %ld",[dog2 ID],[dog2 retainCount]);
-
- NSLog(@"//sim离开");
- [sim release];
- NSLog(@"dog %d retainCount7 is %ld",[dog1 ID],[dog1 retainCount]);
- NSLog(@"dog %d retainCount7 is %ld",[dog2 ID],[dog2 retainCount]);
-
- NSLog(@"//sian离开");
- [sian release];
- NSLog(@"dog %d retainCount8 is %ld",[dog1 ID],[dog1 retainCount]);
- NSLog(@"dog %d retainCount8 is %ld",[dog2 ID],[dog2 retainCount]);
-
- NSLog(@"//dog1 and dog2 release");
- [dog1 release];
- [dog2 release];
-
- }
- return 0;
- }
复制代码 输出结果为:- 2014-01-12 11:50:01.862 Memory_retain[1767:303] //初始化后的retainCount值
- 2014-01-12 11:50:01.864 Memory_retain[1767:303] dog 1 retainCount1 is 1
- 2014-01-12 11:50:01.864 Memory_retain[1767:303] dog 2 retainCount1 is 1
- 2014-01-12 11:50:01.865 Memory_retain[1767:303] //sim开始遛狗dog1
- 2014-01-12 11:50:01.865 Memory_retain[1767:303] dog 1 retainCount2 is 2
- 2014-01-12 11:50:01.865 Memory_retain[1767:303] dog 2 retainCount2 is 1
- 2014-01-12 11:50:01.866 Memory_retain[1767:303] //sian开始遛狗dog2
- 2014-01-12 11:50:01.866 Memory_retain[1767:303] dog 1 retainCount3 is 2
- 2014-01-12 11:50:01.867 Memory_retain[1767:303] dog 2 retainCount3 is 2
- 2014-01-12 11:50:01.867 Memory_retain[1767:303] //sim和sian交换遛狗
- 2014-01-12 11:50:01.867 Memory_retain[1767:303] dog 1 retainCount4 is 2
- 2014-01-12 11:50:01.868 Memory_retain[1767:303] dog 2 retainCount4 is 2
- 2014-01-12 11:50:01.868 Memory_retain[1767:303] //sim和sian都转为遛狗dog1
- 2014-01-12 11:50:01.868 Memory_retain[1767:303] dog 1 retainCount5 is 3
- 2014-01-12 11:50:01.868 Memory_retain[1767:303] dog 2 retainCount5 is 1
- 2014-01-12 11:50:01.869 Memory_retain[1767:303] //sim和sian都转为遛狗dog2
- 2014-01-12 11:50:01.869 Memory_retain[1767:303] dog 1 retainCount6 is 1
- 2014-01-12 11:50:01.879 Memory_retain[1767:303] dog 2 retainCount6 is 3
- 2014-01-12 11:50:01.879 Memory_retain[1767:303] //sim离开
- 2014-01-12 11:50:01.880 Memory_retain[1767:303] Person is dealloc
- 2014-01-12 11:50:01.880 Memory_retain[1767:303] dog 1 retainCount7 is 1
- 2014-01-12 11:50:01.880 Memory_retain[1767:303] dog 2 retainCount7 is 2
- 2014-01-12 11:50:01.881 Memory_retain[1767:303] //sian离开
- 2014-01-12 11:50:01.881 Memory_retain[1767:303] Person is dealloc
- 2014-01-12 11:50:01.881 Memory_retain[1767:303] dog 1 retainCount8 is 1
- 2014-01-12 11:50:01.882 Memory_retain[1767:303] dog 2 retainCount8 is 1
- 2014-01-12 11:50:01.882 Memory_retain[1767:303] //dog1 and dog2 release
- 2014-01-12 11:50:01.883 Memory_retain[1767:303] dog 1 is dealloc
- 2014-01-12 11:50:01.883 Memory_retain[1767:303] dog 2 is dealloc
- Program ended with exit code: 0
复制代码
|
|