From 11b53e1b9f9960fbf56d762e6fbf350e5e796df8 Mon Sep 17 00:00:00 2001 From: mguesdon Date: Tue, 6 Jan 2004 19:43:29 +0000 Subject: [PATCH] 2004-01-06 Manuel Guesdon * Headers/Foundation/NSArray.h/.m: added -setValue:forKey: and -valueForKey: to comply to Mac OS X v10.3 and later documentation. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@18322 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 5 +++ Headers/Foundation/NSArray.h | 5 +++ Source/NSArray.m | 65 ++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) diff --git a/ChangeLog b/ChangeLog index c0b0c76cf..95ee9ea49 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2004-01-06 Manuel Guesdon + * Headers/Foundation/NSArray.h/.m: added -setValue:forKey: + and -valueForKey: to comply to Mac OS X v10.3 and + later documentation. + 2003-12-30 Fred Kiefer * Headers/Foundation/NSGeometry.h diff --git a/Headers/Foundation/NSArray.h b/Headers/Foundation/NSArray.h index ed864c220..f0e95c2bc 100644 --- a/Headers/Foundation/NSArray.h +++ b/Headers/Foundation/NSArray.h @@ -101,6 +101,7 @@ - (BOOL) writeToFile: (NSString*)path atomically: (BOOL)useAuxiliaryFile; #ifndef STRICT_OPENSTEP - (BOOL) writeToURL: (NSURL*)url atomically: (BOOL)useAuxiliaryFile; +- (id) valueForKey: (NSString*)key; #endif @end @@ -142,6 +143,10 @@ context: (void*)context; - (void) sortUsingSelector: (SEL)comparator; +#ifndef STRICT_OPENSTEP +- (void) setValue: (id)value forKey: (NSString*)key; +#endif + @end @interface NSArray (GNUstep) diff --git a/Source/NSArray.m b/Source/NSArray.m index d8fd4e7b0..47df04db9 100644 --- a/Source/NSArray.m +++ b/Source/NSArray.m @@ -43,6 +43,8 @@ #include "Foundation/NSMapTable.h" #include "Foundation/NSLock.h" #include "Foundation/NSDebug.h" +#include "Foundation/NSValue.h" +#include "Foundation/NSNull.h" #include "GNUstepBase/GSCategories.h" #include "GSPrivate.h" @@ -1130,6 +1132,53 @@ compare(id elem1, id elem2, void* context) return [data writeToURL: url atomically: useAuxiliaryFile]; } +/** + * This overrides NSObjects implementation of this method. + * This method returns an array of objects returned by + * invoking -valueForKey: for each item in the receiver, + * substituting NSNull for nil. + * A special case: the key "count" is not forwarded to each object + * of the receiver but returns the number of objects of the receiver.
+ */ +- (id) valueForKey: (NSString*)key +{ + id result=nil; + + if ([key isEqualToString: @"count"] == YES) + { + result = [NSNumber numberWithUnsignedInt: [self count]]; + } + else + { + NSMutableArray *results = nil; + NSNull* null=nil; + + int i, count = [self count]; + volatile id object = nil; + + results = [NSMutableArray array]; + + for(i = 0; i < count; i++) + { + id result; + + object = [self objectAtIndex: i]; + result = [object valueForKey: key]; + if (!result) + { + if (!null) + null=[NSNull null]; + result = null; + } + + [results addObject: result]; + } + + result=results; + } + return result; +} + @end @@ -1723,6 +1772,22 @@ compare(id elem1, id elem2, void* context) #endif } +/** + * Call setValue:forKey: on each of the receiver's items + * with the value and key. + */ +- (void) setValue: (id)value forKey: (NSString*)key +{ + int i, count = [self count]; + volatile id object = nil; + + for(i = 0; i < count; i++) + { + object = [self objectAtIndex: i]; + [object setValue: value + forKey: key]; + } +} @end @interface NSArrayEnumerator : NSEnumerator