diff --git a/ChangeLog b/ChangeLog index 3a444e0fa..ba95b7e36 100644 --- a/ChangeLog +++ b/ChangeLog @@ -5,7 +5,8 @@ * Source/NSUserDefaults.m: Use distributed lock to ensure that there is no possible window when the defaults file is invalid ... not all systems guarantee that the rename() system call is atomic. - + * Source/NSArray.m: New MacOS-X method ([-initWithArray:copyItems:]) + 2002-08-25 Richard Frith-Macdonald * Source/WindowsFileHandle.m: Removed ... no longer used. diff --git a/Headers/gnustep/base/NSArray.h b/Headers/gnustep/base/NSArray.h index dd9bd4124..2b4aeb30f 100644 --- a/Headers/gnustep/base/NSArray.h +++ b/Headers/gnustep/base/NSArray.h @@ -50,6 +50,9 @@ - (unsigned) indexOfObjectIdenticalTo: (id)anObject; - (unsigned) indexOfObjectIdenticalTo: (id)anObject inRange: (NSRange)aRange; - (id) initWithArray: (NSArray*)array; +#ifndef STRICT_OPENSTEP +- (id) initWithArray: (NSArray*)array copyItems: (BOOL)shouldCopy; +#endif - (id) initWithContentsOfFile: (NSString*)file; - (id) initWithObjects: firstObject, ...; - (id) initWithObjects: (id*)objects count: (unsigned)count; // Primitive diff --git a/Source/NSArray.m b/Source/NSArray.m index 64af1d23a..fddad9f3c 100644 --- a/Source/NSArray.m +++ b/Source/NSArray.m @@ -459,26 +459,59 @@ static SEL rlSel; } /** - * Initialize the array with the content of anArray. The order is preserved. - *
May change the value of self before returning it. + * Initialize the receiver with the contents of array. + * The order of array is preserved.
+ * If shouldCopy is YES then the objects are copied + * rather than simply retained.
+ * Invokes -initWithObjects:count: */ -- (id) initWithArray: (NSArray*)array +- (id) initWithArray: (NSArray*)array copyItems: (BOOL)shouldCopy { - unsigned c; + unsigned c = [array count]; + id objects[c]; - c = [array count]; - { - id objects[c]; + [array getObjects: objects]; + if (shouldCopy == YES) + { + unsigned i; - [array getObjects: objects]; - self = [self initWithObjects: objects count: c]; - } + for (i = 0; i < c; i++) + { + objects[i] = [objects[i] copy]; + } + self = [self initWithObjects: objects count: c]; +#if GS_WITH_GC == 0 + while (i > 0) + { + [objects[--i] release]; + } +#endif + } + else + { + self = [self initWithObjects: objects count: c]; + } return self; } /** - * Initialize the array by decoding from an archive. - *
May change the value of self before returning it. + * Initialize the receiver with the contents of array. + * The order of array is preserved.
+ * Invokes -initWithObjects:count: + */ +- (id) initWithArray: (NSArray*)array +{ + unsigned c = [array count]; + id objects[c]; + + [array getObjects: objects]; + self = [self initWithObjects: objects count: c]; + return self; +} + +/** + * Initialize the array by decoding from an archive.
+ * Invokes -initWithObjects:count: */ - (id) initWithCoder: (NSCoder*)aCoder { @@ -496,7 +529,9 @@ static SEL rlSel; return [self initWithObjects: contents count: count]; } else - return [self initWithObjects: 0 count: 0]; + { + return [self initWithObjects: 0 count: 0]; + } } /** @@ -548,8 +583,9 @@ static SEL rlSel; } /** - * Initialize the array with count objects. - *
May change the value of self before returning it. + * Initialize the array with count objects.
+ * Retains each object placed in the array.
+ * Like all initializers, may change the value of self before returning it. */ - (id) initWithObjects: (id*)objects count: (unsigned)count {