合成属性改变行为

时间:2011-07-14 16:18:21

标签: iphone objective-c

我有很多这样的属性: Myobject.property

现在我想更改这些属性的计算方式。我可以这样做,同时仍然保持。语法?

1 个答案:

答案 0 :(得分:4)

是的,您可以在.h文件中使用@property定义属性,而不是在.m文件中使用@synthesize,您只需定义自己的getter / setter:

@interface YourClass : NSObject {
        @private NSString* iVar_;
}

@property (readwrite, assign) NSString* iVar;
@end

然后在你的.m:

@implementation YourClass 

- (NSString*) iVar {
    // Put your custom logic here.
    return iVar_;
}

- (void) setIVar:(NSString*)value {
    // Put your custom logic here.
    // Note: be aware of retain counts... you may need to call release on iVar before assigning it a new value.
    iVar_ = value;
}

@end