Mailing Lists: Apple Mailing Lists

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

valueForKey and NSArray



Hi list,
We know given a dictionary like
foo = { 
            a = ...
            b = ( {result = ok; date = d1; }, { result = good; date = d6;}, { result = bad; date = d3;});
            ...
          }
[foo valueForKeyPath:@"b.result"] will return an array (ok,good,bad)

---------------------------------
I'm trying to make 

[dict valueForKeyPath: @"b.1.date"] returning  'd6' and
[dict valueForKeyPath: @"b.0.result"] returning  'ok'

It seems to me that all i need to do is a category method 
- (id) valueForKey:(NSString *)aKey;
for NSArray that do the same thing as usual except when aKey is representing an integer:
when aKey represents an integer, the method returns the object at the index of that integer
if possible, otherwise nil.

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

Any ideas?


 _______________________________________________
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



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.