Re: NSArray operator implementing
Re: NSArray operator implementing
- Subject: Re: NSArray operator implementing
- From: mmalcolm crawford <email@hidden>
- Date: Fri, 26 Nov 2004 12:18:33 -0800
Op 23-nov-04 om 0:59 heeft Cornelius Jaeger het volgende geschreven:
what i need is a @flatten operator, that will take a keypath and
flatten the arrays within.
I didn't see the original post, so I'm not completely clear on the
context, however is this the sort of thing you want?
mmalc
#import <Foundation/Foundation.h>
@implementation NSArray (Flattened)
- (NSArray *)flattened
{
int count = [self count], i;
NSMutableArray *array = [NSMutableArray arrayWithCapacity:count];
Class nsarrayClass = [NSArray class];
for (i = 0; i < count; i++) {
id object = [self objectAtIndex:i];
if (![object isKindOfClass:nsarrayClass]) {
[array addObject:object];
}
else {
[array addObjectsFromArray:[object flattened]];
}
}
return array;
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *mutableArray =
[NSMutableArray arrayWithObjects:@"aa", @"ab", @"ac", nil];
[mutableArray addObject:
[NSArray arrayWithObjects:@"ba", @"bb", @"bc", nil]];
[mutableArray addObject:
[NSArray arrayWithObjects:@"ca", @"cb", @"cc",
[NSArray arrayWithObjects:@"cda", @"cdb", @"cdc", nil], nil]];
NSDictionary *dict = [NSDictionary dictionaryWithObject:mutableArray
forKey:@"mutableArray"];
NSLog(@"mutableArray : %@", mutableArray);
NSLog(@"valueForKeyPath:@\"mutableArray.@flattened\" : %@", [dict
valueForKeyPath:@"mutableArray.@flattened"]);
[pool release];
return 0;
}
_______________________________________________
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