OC6_复合类的类存管理
// // Person.h // OC6_复合类的类存管理 // // Created by zhangxueming on 15/6/18. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> #import "Dog.h" @interface Person : NSObject @property (retain, nonatomic) Dog *dog; @end // // Person.m // OC6_复合类的类存管理 // // Created by zhangxueming on 15/6/18. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "Person.h" @implementation Person - (void)dealloc { NSLog(@"%@:dog is release", [self class]); [_dog release]; [super dealloc]; } @end
// // Dog.h // OC6_复合类的类存管理 // // Created by zhangxueming on 15/6/18. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> @interface Dog : NSObject @property (copy, nonatomic) NSString *name; @property (assign,nonatomic) NSInteger age; @end // // Dog.m // OC6_复合类的类存管理 // // Created by zhangxueming on 15/6/18. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "Dog.h" @implementation Dog - (void)dealloc { NSLog(@"%@: dog name is release!!!", [self class]); [_name release]; [super dealloc]; } @end
// // main.m // OC6_复合类的类存管理 // // Created by zhangxueming on 15/6/18. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> #import "Person.h" #import "Dog.h" int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... //NSLog(@"Hello, World!"); Person *xiaoHua = [[Person alloc] init]; Dog *xiaoHei = [[Dog alloc] init]; xiaoHei.name = @"小黑"; //setter方法 xiaoHua.dog = xiaoHei ; // [xiaoHei retain] NSLog(@"%li----",xiaoHua.retainCount); NSLog(@"xiao%li----",xiaoHei.retainCount); Person *p =[xiaoHua retain]; // NSLog(@"xiaohei%li----",xiaoHei.retainCount); // NSLog(@"tt%li----",p.retainCount); //[xiaoHua release]; NSLog(@"最后%li----",xiaoHei.retainCount); [xiaoHei release]; } return 0; }