OC MRC之循环引用问题(代码分析)

时间:2021-03-06 19:25:19
 //
// main.m
// 07-循环引用
//
// Created by apple on 13-8-9.
// Copyright (c) 2013年 itcast. All rights reserved.
// /*
1.@class的作用:仅仅告诉编译器,某个名称是一个类
@class Person; // 仅仅告诉编译器,Person是一个类 2.开发中引用一个类的规范
1> 在.h文件中用@class来声明类
2> 在.m文件中用#import来包含类的所有东西 3.两端循环引用解决方案
1> 一端用retain
2> 一端用assign */ #import <Foundation/Foundation.h>
#import "Card.h"
// @class仅仅是告诉编译器,Card是一个类
//@class Card; @interface Person : NSObject @property (nonatomic, retain) Card *card; @end #import "Person.h"
#import "Card.h" @implementation Person - (void)dealloc
{
NSLog(@"Person被销毁了");
[_card release]; [super dealloc];
} @end #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(@"Car被销毁了"); // [_person release]; [super dealloc];
} @end #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 ;
}