Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: valueForKey and NSArray



On 6/24/06, YL <email@hidden> wrote:
But I don't know how to state "do it as usual" in code:-(

@implementation NSArray (keyPath)
- (id) valueForKey:(NSString *)aKey;
{
    if([aKey isEqual:[NSString stringWithFormat:@"%d",[aKey intValue]]]) {
        if([self count] > [aKey intVaue])
            return [self objectAtIndex:[aKey intValue]];
        return   RETURNVALUE_AS_USUAL;
}
@end

"Do it as usual" is generally done by invoking super's implementation. However, in a category on NSArray, super's implementation is NSObject, which is not what you want. This is a problem with categories, in that it's impossible to call the original implementation of a method you replace.

To get around this, either investigate using poseAsClass: or method
swizzling (http://www.cocoadev.com/index.pl?MethodSwizzling). Both of
these techniques allow you to call the old implementation in your
override.

Mike
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >valueForKey and NSArray (From: "YL" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.