雖然學習Objective C我已經習慣了@property和@synthesize是如何工作的。iPhone合成
//usual way
@synthesize world;
但最近我遇到實踐是@synthesize是這樣的:
//new way
@synthesize world=world_;
有人能解釋爲什麼這種技術使用嗎?
感謝,
馬克
雖然學習Objective C我已經習慣了@property和@synthesize是如何工作的。iPhone合成
//usual way
@synthesize world;
但最近我遇到實踐是@synthesize是這樣的:
//new way
@synthesize world=world_;
有人能解釋爲什麼這種技術使用嗎?
感謝,
馬克
我相信這是爲了避免實際的伊娃名和屬性名之間的混淆。 所以你可以有
@interface MyClass : NSObject
{
NSObject *_myObjActualIVar;
}
@property (retain) NSObject *myObjProperty;
@end
@implementation MyClass
@synthesize myObjProperty = _myObjActualIVar;
- (void) someMethod: (NSObject *)someOtherObject
{
[_myObjActualIVar release]; // here I know I'm accessing the ivar
self.myObjProperty = someOtherObject; // here I know I'm using the setter method
//myObjProperty = someOtherObject; // if I accidentally try to do this
//self._myObjActualIVar; // or this, the compiler will tell me
}
@end
,然後它使你更難或其他一些開發商對這類工作,不慎訪問當你真正想要的屬性或反之亦然伊娃。不同的名字使得它更清楚哪個是哪個。
可以使用表格屬性=的ivar到 指示特定實例 變量應該被用於 屬性,例如:
@synthesize firstName, lastName, age = yearsOld