标签:内存管理
创建一个BOOK对象,对其属性进行声明 定义。
@property 属性声明 定义了对属性的赋值
-(void) dealloc 方法在对象销毁的时候进行调用;
#import <Foundation/Foundation.h> @interface Book : NSObject @property (nonatomic,assign) int price; @end
#import "Book.h" @implementation Book -(void)dealloc { NSLog(@"Book is dealloc"); [super dealloc]; } @end
创建一个Person对象,对其属性进行声明 定义。
@property 属性声明 定义了对属性的引用计数加一并在-(void) dealloc 方法在对象销毁的时候减一;
#import <Foundation/Foundation.h> #import "Book.h" @interface Person : NSObject @property (nonatomic ,retain)Book *book; @end
#import "Person.h" @implementation Person -(void)dealloc { [_book release]; NSLog(@"Person is dealloc"); [super dealloc]; } @end
#import <Foundation/Foundation.h> #import "Person.h" #import "Book.h" int main(int argc, const char * argv[]) { Person *p=[[Person alloc] init]; Book *b=[[Book alloc] init]; b.price=45; p.book=b; [b release]; NSLog(@"%d ",p.book.price ); [p release]; return 0; }
@property属性的参数分别为
assign,retain,copy-针对引用计数,assign为默认,并在非oc对象作为属性时使用
nonautomic,automic-在线程中起一定的作用,一般nonautomic效率会高点,默认是automic
readonly,readwrite-对属性进行setter getter存储,一般默认是readwrite
标签:内存管理
原文地址:http://blog.csdn.net/qq285016127/article/details/30137735