Skip navigation.
 
mlRe: NSArray operator implementing
FROM : mmalcolm crawford
DATE : Fri Nov 26 21:18:33 2004

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;
}

Related mailsAuthorDate
mlNSArray operator implementing Cornelius Jaeger Nov 23, 00:59
mlRe: NSArray operator implementing Steven Kramer Nov 26, 11:51
mlRe: NSArray operator implementing mmalcolm crawford Nov 26, 21:18
mlRe: NSArray operator implementing mmalcolm crawford Nov 27, 20:52
mlRe: NSArray operator implementing Steven Kramer Nov 29, 15:10