Skip navigation.
 
mlRe: KVO autonotifying complaining about custom setter return value
FROM : Adam P Jenkins
DATE : Wed Mar 05 22:40:58 2008

On Mar 5, 2008, at 2:40 PM, Jim Turner wrote:

> On Wed, Mar 5, 2008 at 12:00 PM, mmalc crawford 
> <<email_removed>> wrote:

>>
>> On Mar 5, 2008, at 9:34 AM, Jim Turner wrote:
>>

>>> I filed a bug (rdar://problems/5781977) as this doesn't appear to be
>>> proper behavior.  I'd be happy to be told I'm wrong if you can point
>>> out what I'm missing.

>>
>> I believe this behaves correctly.
>> As stated in <http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_5_section_5.html#

>>> , "Key-value coding and declared properties are orthogonal
>> technologies."
>> KVC doesn't know about any custom setter you may have defined for a
>> property.
>>
>> mmalc
>>
>>

>
> Hmm, it appears the developer documentation I have locally on my
> machine is slightly out of date.  After reading that link (and the
> updated description of setter= and getter=), I see now why what I'm
> doing isn't working.  Properties and KVO/KVC aren't complimentary to
> each other... although it'd be nice if they were.  I'll have to
> re-work my object to get it to be properly compliant.
>
> But, I still appear to have an issue with defining a custom
> getter/setter.  Defining a property as
>
> @property (setter=mySetMethod:,getter=myMethod) id valueTest;
>
> and sending my object a valueTest message, I get the unrecognized
> selector sent to instance warning.  Reading (and re-reading several
> times) http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_5_section_3.html#/
> /apple_ref/doc/uid/TP30001163-CH17-SW17
> it appears that I don't need to define anything other than the
> @property but unless I also place
>
> -(id) valueTest;
> -(void) setValueTest:(id)_value;
>
> in the interface, the object can't find a method signature.
>
> This is a little more confusing that I originally thought. I
> appreciate all the help, though.


If you define a @property in the interface, then in the implementation 
you either need use @synthesize to have the compiler automatically 
generate a getter and setter, or use @dynamic to inform the compiler 
that you will provide the appropriately named getter and setter 
methods.  In neither case should there be a need to declare the 
getter and setter methods explicitly in your interface, since the 
@property directive does that for you.  See here for more info: http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_5_section_1.html

For example:

@interface Person : NSObject
{
  NSString *firstName;
}
@property(copy) NSString *firstName;
@end

@implementation Person
@synthesize firstName;  // causes firstName and setFirstName: methods 
to be generated
@end

This defines Person with a KVC compliant firstName property.    You 
could now access the firstName property using either person.firstName 
and person.firstName=@"Joe", or [person firstName] and [person 
setFirstName:@"Joe"].

If you need to define the accessors yourself because the auto-
generated ones don't do what you want, then use @dynamic instead like 
this:

@implementation Person
@dynamic firstName;

- (NSString*)firstName { ... }
- (void)setFirstName:(NSString *)newName { ... }
@end