我已经创建了一个setter模块,当任何东西被存储到_textColor变量时它应该被调用但是它不起作用,如果我删除新的setter模块并让合成器创建默认模块然后默认的那些模块将工作。这个问题是,当我有一个名为'fillColor'的变量时,我最终会访问一个名为setFillColor的不同模块:
我省略了大部分无关的代码 在我的头文件中,我有以下内容:
@interface ICADrawingObject : NSObject <NSCoding> {
@private
NSColor *_textColor;
}
@property (nonatomic, retain, readwrite, setter=setObjectTextColor:) NSColor* textColor;
在实施中我有:
@synthesize textColor = _textColor;
-(void)setObjectTextColor:(NSColor *)textColor{
NSLog(@"Text Old Color: %@",self.textColor);
NSLog(@"Text New Color: %@",textColor);
_textColor = textColor;
}
答案 0 :(得分:0)
就像这样:
-(void)setTextColor:(NSColor *)textColor{
if (_textColor != textColor) {
[_textColor release];
_textColor = [textColor retain];
}
答案 1 :(得分:0)
这个怎么样:
@interface ICADrawingObject : NSObject {
NSColor *_textColor;
}
@property (nonatomic, retain, setter=setObjectTextColor:) NSColor *textColor;
@end
@implementation ICADrawingObject
@synthesize textColor = _textColor;
-(void)setObjectTextColor:(NSColor *)aColor{
NSLog(@"Text Old Color: %@",_textColor);
NSLog(@"Text New Color: %@",aColor);
if( _textColor != aColor ) {
[_textColor release];
_textColor = [aColor retain];
}
_textColor = aColor;
}
@end
此后,如果我们这样做:
ICADrawingObject *obj = [ICADrawingObject new];
obj.textColor = [NSColor blackColor];
然后这应该打印到控制台,作为调用自定义setter的证据:
2012-09-19 08:37:43.605 test323[67540:303] Text Old Color: (null)
2012-09-19 08:37:43.606 test323[67540:303] Text New Color: NSCalibratedWhiteColorSpace 0 1
顺便说一句,没有必要在ivars上进行@private
指定。您现在可以通过在实现中声明ivars来控制可见性。