内存管理2-@class关键字

Review:

给对象发送消息,进行相应的计数器操作。

Retain消息:使计数器+1,改方法返回对象本身

Release消息:使计数器-1(并不代表释放对象)

retainCount消息:获得对象当前的引用计数器值

Management principles:

1 alloc new copy

who create ,who release

2 except (alloc new copy)

Please state autorelease

3 Who retain,who release (anyway)

------------------------------------------------------

 @class  ---->Key word

// Add content

//When use dealloc

-(void)dealloc{

//self.book=nil;->OK

//[self setBook:nil];->OK

[_book release];

[super dealloc];

}

//Because self.book =setBook . It's a method inside can release book,not visiting //mumber variable.

-(void) setBook:(Book *)book{

if(_book!=book){

[_book realease];

_book=[book retain];

}

}

 ------------------------------------------------------

@class

In Student.h file

General in development #import "Book.h" will cause some problem in property.

#import "Book.h"

will copy all the things and most of them is useless.

And we want only know Book is a class,I don't want know other things(inheritation&getter&setter)

So in Student.h

Replace  #import "Book.h"; as @class Book;

 

原文地址:https://www.cnblogs.com/yesihoang/p/4487696.html