• 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
overriding a method with NSObject's version
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

overriding a method with NSObject's version


  • Subject: overriding a method with NSObject's version
  • From: Roland King <email@hidden>
  • Date: Sat, 24 Nov 2012 17:55:35 +0800

I have a fairly well-used and oft-subclassed class which overrides valueForKey: and setValue:ForKey: to behave like the NSDictionary versions, because this class looks much like an NSDictionary. I have a subclass of that which wants to do 'normal' KVO, and so I need valueForKey: and setValueForKey: to return to the default NSObject version.

I could use composition but that's a lot of boilerplate to re-expose the properties of the composed class and I really did want it to be a subclass. I could code up a custom valueForKey: which does the right thing, but that's fragile if I add more properties or subclass again. So I want to actually make the implementation of those two methods in my subclass use the NSObject implementation.

I've messed about with +resolveClassMethod, -methodForSelector and +instanceMethodForSelector, but found no fruit there, none of them called for valueForKey:, so currently I have this implementation

-(id)getValueForKey:(NSString*)key
{
	static IMP valueForKeyImplementation = NULL;
	if( !valueForKeyImplementation )
		valueForKeyImplementation = [ NSObject instanceMethodForSelector:@selector( valueForKey: ) ];
	return valueForKeyImplementation( self, _cmd, key );
}

which has a faint code odour about it. I'd prefer to find a hook where the runtime asks me 'hello, do you have a custom IMP for this method valueForKey:' and I can say 'yes I do and here it is', having stolen it from NSObject in the same way. That just feels like a more collaborative way of doing it. Is there one?

_______________________________________________

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: overriding a method with NSObject's version
      • From: "email@hidden" <email@hidden>
  • Prev by Date: NSComparisonPredicate vs. predicateWithFormat
  • Next by Date: Re: NSComparisonPredicate vs. predicateWithFormat
  • Previous by thread: Re: NSComparisonPredicate vs. predicateWithFormat
  • Next by thread: Re: overriding a method with NSObject's version
  • Index(es):
    • Date
    • Thread