简体   繁体   中英

Setter methods not being called

I've created a setter module that that should get called when anything is being stored into the _textColor variable but it isn't working, if I remove the new setter module and have synthesizer create default ones then the default ones will work. The problem with this is that when I have a variable called 'fillColor' I end up getting access to a different module called setFillColor:

I've omitted the bulk of code which is irrelevant In my header file I have the following:

@interface ICADrawingObject : NSObject <NSCoding> {
@private
    NSColor                 *_textColor;
}
@property (nonatomic, retain, readwrite, setter=setObjectTextColor:) NSColor* textColor;

In the implementation I have:

@synthesize textColor = _textColor;

-(void)setObjectTextColor:(NSColor *)textColor{
    NSLog(@"Text Old Color: %@",self.textColor);
    NSLog(@"Text New Color: %@",textColor);

    _textColor = textColor;
}

It would be like this:

 -(void)setTextColor:(NSColor *)textColor{
    if (_textColor != textColor) {
    [_textColor release];
    _textColor = [textColor retain];

 }

How about this:

@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

Thereafter, if we do this:

ICADrawingObject *obj = [ICADrawingObject new];
obj.textColor = [NSColor blackColor];

then this should print to the console, as evidence that the custom setter is being called:

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

By the way, there's no need for the @private designation on ivars. You can control the visibility now by declaring ivars in the implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM