1 // 2 // main.m 3 // 07-循环引用 4 // 5 // Created by apple on 13-8-9. 6 // Copyright (c) 2013年 itcast. All rights reserved. 7 // 8 9 10 /* 11 1.@class的作用:仅仅告诉编译器,某个名称是一个类 12 @class Person; // 仅仅告诉编译器,Person是一个类 13 14 2.开发中引用一个类的规范 15 1> 在.h文件中用@class来声明类 16 2> 在.m文件中用#import来包含类的所有东西 17 18 3.两端循环引用解决方案 19 1> 一端用retain 20 2> 一端用assign 21 22 */ 23 24 25 #import <Foundation/Foundation.h> 26 #import "Card.h" 27 // @class仅仅是告诉编译器,Card是一个类 28 //@class Card; 29 30 @interface Person : NSObject 31 32 @property (nonatomic, retain) Card *card; 33 34 @end 35 36 #import "Person.h" 37 #import "Card.h" 38 39 @implementation Person 40 41 - (void)dealloc 42 { 43 NSLog(@"Person被销毁了"); 44 [_card release]; 45 46 [super dealloc]; 47 } 48 49 @end 50 51 #import <Foundation/Foundation.h> 52 53 @class Person; 54 55 @interface Card : NSObject 56 57 @property (nonatomic, assign) Person *person; 58 59 @end 60 61 #import "Card.h" 62 #import "Person.h" 63 64 @implementation Card 65 66 67 - (void)dealloc 68 { 69 NSLog(@"Car被销毁了"); 70 71 // [_person release]; 72 73 [super dealloc]; 74 } 75 76 @end 77 78 79 80 #import <Foundation/Foundation.h> 81 #import "Card.h" 82 #import "Person.h" 83 84 int main() 85 { 86 // p - 1 87 Person *p = [[Person alloc] init]; 88 // c - 1 89 Card *c = [[Card alloc] init]; 90 91 // c - 2 92 p.card = c; 93 94 // p - 1 95 c.person = p; 96 97 // c - 1 98 [c release]; 99 100 // p - 0 c - 0 101 [p release]; 102 return 0; 103 }