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