Should setter generated with @synthesize be KVC compilant or not? I found statement that getters and setters generated are KVC-compliant, shouldn’t it call one of this methods?
@interface testing : NSObject
@property (nonatomic, retain) NSString *phone;
@end
implementation:
@implementation testing
@synthesize phone;
- (id)init {
self = [super init];
return self;
}
// none of these is called with dot syntax, or setter setPhone
- (void)setValue:(id)value forKey:(NSString *)key
{
NSLog(@"%@",key);
[super setValue:value forKey:key];
}
-(void)setValue:(id)value forKeyPath:(NSString *)keyPath
{
NSLog(@"%@",keyPath);
[super setValue:value forKeyPath:keyPath];
}
@end
and test it with:
testing *t = [[testing alloc] init];
[t setPhone:@"55555555"];
I think you’ve got it the wrong way round.. KVC compliant doesn’t mean that an accessor will call
-setValue:forKey:Being KVC compliant means that calling-setValue:forKey:will call the accessor.Expanding a bit: KVC compliant only means ‘follows naming conventions‘. Why is this important? I can call my accessor methods anything i like. For a property ‘Foo’:
This is fine. But a mechanism like Bindings will try to set Foo by calling
-setValue:forKey:will try to do the right thing and use the accessor method (if we wrote a setter method, it’s because we want it to be used, right?). But unless we named our setter method the standard-setFoo:there’s no way it will be found.So
-weakSetFoo:is a setter method, but the property Foo isn’t KVC compliant.If i change the setter name to
-setFoo:the property Foo is now KVC compliant.Synthesized accessor methods will by default be named correctly.