@class
循环引用:你引用我,我引用你
1>@class的作用:仅仅告诉编译器有这么一个类,并不知道里面有什么
*@class person:告诉编译器有person这个类
2>开发中引用一个类的规范:为什么这样写(面试题):跟性能有关,可以循环引用、#import作用是拷贝,@class还可以提高编译效率
*在.h文件中用@class申明
*在.m文件中用#import来包含类中所有的东西
3>两端循环引用的解决方法
*一端用assign
*一端用retain
1 #import2 #import "Person.h" 3 #import "Card.h" 4 int main(int argc, const char * argv[]) { 5 Person *p = [[Person alloc]init]; 6 Card *c = [[Card alloc]init]; 7 p.card = c; 8 c.person = p; 9 [c release];10 [p release];11 return 0;12 }
1 #import2 //#import "Card.h"3 @class Card;4 @interface Person : NSObject5 @property(nonatomic,assign)Card *card;6 7 8 @end
1 #import "Person.h" 2 #import "Card.h" 3 @implementation Person 4 5 - (void)dealloc{ 6 7 // [_card release]; 8 // [_card release]; 9 NSLog(@"person对象被销毁");10 // [_card release];11 [super dealloc];12 }13 14 @end
1 #import2 //#import "Person.h"3 @class Person;4 @interface Card : NSObject5 @property(nonatomic,retain)Person *person;6 @end
1 #import "Card.h" 2 #import "Person.h" 3 @implementation Card 4 5 - (void)dealloc{ 6 [_person release]; 7 // [_person release]; 8 NSLog(@"card对象被销毁"); 9 [super dealloc];10 }11 @end