valueForKey and NSArray
valueForKey and NSArray
- Subject: valueForKey and NSArray
- From: "YL" <email@hidden>
- Date: Sat, 24 Jun 2006 18:15:20 -0600
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:
This email sent to email@hidden