• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: KVO autonotifying complaining about custom setter return value
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: KVO autonotifying complaining about custom setter return value


  • Subject: Re: KVO autonotifying complaining about custom setter return value
  • From: Adam P Jenkins <email@hidden>
  • Date: Wed, 5 Mar 2008 16:40:58 -0500

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

On Wed, Mar 5, 2008 at 12:00 PM, mmalc crawford <email@hidden> 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


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: KVO autonotifying complaining about custom setter return value
      • From: mmalc crawford <email@hidden>
References: 
 >KVO autonotifying complaining about custom setter return value (From: "Jim Turner" <email@hidden>)
 >Re: KVO autonotifying complaining about custom setter return value (From: "Kyle Sluder" <email@hidden>)
 >Re: KVO autonotifying complaining about custom setter return value (From: Chris Suter <email@hidden>)
 >Re: KVO autonotifying complaining about custom setter return value (From: "Jim Turner" <email@hidden>)
 >Re: KVO autonotifying complaining about custom setter return value (From: mmalc crawford <email@hidden>)
 >Re: KVO autonotifying complaining about custom setter return value (From: "Jim Turner" <email@hidden>)

  • Prev by Date: Re: KVO autonotifying complaining about custom setter return value
  • Next by Date: NSPreferencePane with my own private framework?
  • Previous by thread: Re: KVO autonotifying complaining about custom setter return value
  • Next by thread: Re: KVO autonotifying complaining about custom setter return value
  • Index(es):
    • Date
    • Thread