From 810eb67b402f51d83c2f493a317c3452a1f5e7cb Mon Sep 17 00:00:00 2001 From: Wolfgang Sourdeau Date: Wed, 15 Aug 2007 19:50:20 +0000 Subject: [PATCH] Monotone-Parent: af5a859ee61b26ffd727e648f95c54ee60e3079e Monotone-Revision: 668c4f876568e36b66e3c665c37e3108a08fb967 Monotone-Author: wsourdeau@inverse.ca Monotone-Date: 2007-08-15T19:50:20 Monotone-Branch: ca.inverse.sogo --- ChangeLog | 9 +++++ SoObjects/SOGo/NSArray+Utilities.h | 3 ++ SoObjects/SOGo/NSArray+Utilities.m | 56 ++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/ChangeLog b/ChangeLog index 82a1907c8..3d9c39cbf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2007-08-15 Wolfgang Sourdeau + * SoObjects/SOGo/NSArray+Utilities.m ([NSArray + -keysWithFormat:format]): method that forward the method of the + same name to each member of the array, considering they all are + instances of NSDictionary. + ([NSArray -objectsForKey:key]): same principle as above. + ([NSArray -flattenedArray]): new method that transforms an array + of arrays into a single array containing all the elements of the + subarrays. + * SoObjects/SOGo/NSDictionary+Utilities.m ([NSDictionary -keysWithFormat:keyFormat]): new method inspired by the python string formatting system and which replaces occurences of "%{key}" diff --git a/SoObjects/SOGo/NSArray+Utilities.h b/SoObjects/SOGo/NSArray+Utilities.h index b330cf854..181f636cd 100644 --- a/SoObjects/SOGo/NSArray+Utilities.h +++ b/SoObjects/SOGo/NSArray+Utilities.h @@ -32,6 +32,9 @@ - (NSString *) jsonRepresentation; - (NSArray *) stringsWithFormat: (NSString *) format; +- (NSArray *) keysWithFormat: (NSString *) format; +- (NSArray *) objectsForKey: (NSString *) key; +- (NSArray *) flattenedArray; - (BOOL) containsCaseInsensitiveString: (NSString *) match; diff --git a/SoObjects/SOGo/NSArray+Utilities.m b/SoObjects/SOGo/NSArray+Utilities.m index 77dabdcbd..4845b33df 100644 --- a/SoObjects/SOGo/NSArray+Utilities.m +++ b/SoObjects/SOGo/NSArray+Utilities.m @@ -33,6 +33,7 @@ id currentObject; formattedStrings = [NSMutableArray arrayWithCapacity: [self count]]; + objects = [self objectEnumerator]; currentObject = [objects nextObject]; while (currentObject) @@ -45,6 +46,61 @@ return formattedStrings; } +- (NSArray *) keysWithFormat: (NSString *) format +{ + NSMutableArray *formattedStrings; + NSEnumerator *objects; + id currentObject; + + formattedStrings = [NSMutableArray arrayWithCapacity: [self count]]; + + objects = [self objectEnumerator]; + currentObject = [objects nextObject]; + while (currentObject) + { + [formattedStrings addObject: [currentObject keysWithFormat: format]]; + currentObject = [objects nextObject]; + } + + return formattedStrings; +} + +- (NSArray *) objectsForKey: (NSString *) key +{ + NSMutableArray *objectsForKey; + unsigned int count, max; + id value; + + max = [self count]; + objectsForKey = [NSMutableArray arrayWithCapacity: max]; + + for (count = 0; count < max; count++) + { + value = [[self objectAtIndex: count] objectForKey: key]; + [objectsForKey addObject: value]; + } + + return objectsForKey; +} + +- (NSArray *) flattenedArray +{ + NSMutableArray *flattenedArray; + NSEnumerator *objects; + id currentObject; + + flattenedArray = [NSMutableArray array]; + objects = [self objectEnumerator]; + currentObject = [objects nextObject]; + while (currentObject) + { + [flattenedArray addObjectsFromArray: currentObject]; + currentObject = [objects nextObject]; + } + + return flattenedArray; +} + - (void) makeObjectsPerform: (SEL) selector withObject: (id) object1 withObject: (id) object2