在.h文件中声明@property可以简化代码,自动生成getter和setter方法。

@property (copy) NSString *name;

实现setter方法的值拷贝,如果没有应用ARC(auto garbage collect)需要手动释放

[name release];

@property (retain) Engine *engine;    

引用对象,并且引用计数器+1

@property (readwrite, copy) NSString *name;    

readwrite属性是默认的,可以为了突出而显性写出

@property (readonly, copy) NSString *name;    

使值不能被改变

@property (getter=isHidden) BOOL hidden;

使用自定义的getter的方法名

在.m文件中@synthesize告诉编译器生成accessors,你也可以写自己的accessors。利用@dynamic告诉编译器不生成变量或代码。以下原文:

Use the @synthesize directive to tell the compiler to generate the implementation for the accessors. You can control which instance variable is affected by the generated implementation. If you don’t want to use the default behavior, you’re free to write your own code for the accessors. You can use @dynamic to tell the compiler not to generate a variable or code. 

参考书籍 Learn Objective-C for iOS and OS X, Chapter 11

Copyright © 2012 by Scott Knaster, Waqar Malik, Mark Dalrymple 

posted on 2012-10-11 13:54  Drinking  阅读(167)  评论(0编辑  收藏  举报