Re: overriding a method with NSObject's version
Re: overriding a method with NSObject's version
- Subject: Re: overriding a method with NSObject's version
- From: Ariel Feinerman <email@hidden>
- Date: Thu, 06 Dec 2012 15:13:16 +0400
- (id) valueForKey: (NSString *) {
const char *types = [[NSString stringWithFormat: @"%s%s%s%s", , @encode(id),
@encode(id), @encode(SEL), @encode(id)] UTF8String];
if ([self class] == [MyClass class]) {
IMP function = class_getMethodImplementation([NSObject class], @selector(
valueForKey:));
class_replaceMethod([self class], _cmd, function, types);
}
else {
IMP function = class_getMethodImplementation([OtherClass class],
@selector(valueForKey:));
class_replaceMethod([self class], _cmd, function, types);
}
return [self valueForKey: key];
}
On Sun, Nov 25, 2012 at 6:36 AM, Roland King <email@hidden> wrote:
>
> On 24 Nov, 2012, at 10:23 PM, email@hidden wrote:
>
> >
> >
> > On 24 Nov 2012, at 09:55, Roland King <email@hidden> wrote:
> >
> >> 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.
> >>
> >>
> >
> > Would something like the following be worth considering:
> >
> > Class A uses custom valueForKey.
> > SubClass A1 needs the super implementation.
> >
> > A:
> >
> > + (BOOL)useCustomKVO
> > {
> > return YES;
> > }
> >
>
> That's what I started with and thought, there must be a better way, but
> there isn't a better way I can find that doesn't involve what's basically
> swizzling so .. I'm going to go back to that simple, direct,
> easy-to-understand-and-document version.
>
> Thanks for all the on and off list replies.
>
> _______________________________________________
>
> 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
>
--
best regards
Ariel
_______________________________________________
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