@class循环引用
/*
1. @class的作用:仅仅告诉编译器,某个名称是一个类
@class Person; //仅仅告诉编译器,Person是一个类
2.开发中引用一个类的规范
1> 在.h文件中用@class来声明类
2> 在.m文件中用#import来包含类的所有东西
3.两端循环引用解决方案
1> 一端用retain
2> 一端用assign;
*/
#import <Foundation/Foundation.h>
#import "Card.h"
@interface Person : NSObject
@property (nonatomic,retain) Card *card;
@end
#import "Person.h"
@implementation Person
- (void)dealloc
{
NSLog(@"Person类被销毁了!");
[_card release];
[super dealloc];
}
#import <Foundation/Foundation.h>
@class Person;
@interface Card : NSObject
@property (nonatomic,assign) Person *person;
@end
#import "Card.h"
#import "Person.h"
@implementation Card
- (void)dealloc
{
NSLog(@"Card被销毁了");
//[_person release];
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
Person *p = [[Person alloc] init];
Card *c = [[Card alloc] init];
//c = 2
p.card = c;
//p = 1
c.person = p;
//p = 0, c = 1
[p release];
//c = 0
[c release];
return 0;
}