From 5af66b60e117944784813f80e1a29a7364472769 Mon Sep 17 00:00:00 2001 From: Francisco Ryan Tolmasky I Date: Sun, 9 Jan 2011 03:46:29 -0800 Subject: [PATCH] Better -copy implementations for KVC arrays and sets. Reviewed by me. --- Foundation/CPArray/_CPJavaScriptArray.j | 4 ++-- Foundation/CPKeyValueCoding.j | 14 ++++---------- Foundation/CPSet/CPSet.j | 2 +- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/Foundation/CPArray/_CPJavaScriptArray.j b/Foundation/CPArray/_CPJavaScriptArray.j index 640a9bdf0..6785d8b14 100644 --- a/Foundation/CPArray/_CPJavaScriptArray.j +++ b/Foundation/CPArray/_CPJavaScriptArray.j @@ -35,12 +35,12 @@ var indexOf = Array.prototype.indexOf, - (id)initWithArray:(CPArray)anArray { - return slice.call(anArray, 0); + return [self initWithArray:anArray copyItems:NO]; } - (id)initWithArray:(CPArray)anArray copyItems:(BOOL)shouldCopyItems { - if (!shouldCopyItems) + if (!shouldCopyItems && anArray.isa === _CPJavaScriptArray) return slice.call(anArray, 0); self = [super init]; diff --git a/Foundation/CPKeyValueCoding.j b/Foundation/CPKeyValueCoding.j index c618e6d73..5bba5ffbf 100644 --- a/Foundation/CPKeyValueCoding.j +++ b/Foundation/CPKeyValueCoding.j @@ -347,8 +347,8 @@ var CPObjectAccessorsForClassKey = @"$CPObjectAccessorsForClassKey", - (id)copy { - var objects = [self objectsAtIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(0, [self count])]]; - return [[CPArray alloc] initWithArray:objects]; + // We do this to ensure we return a CPArray. + return [CPArray arrayWithArray:self]; } @end @@ -408,14 +408,8 @@ var CPObjectAccessorsForClassKey = @"$CPObjectAccessorsForClassKey", - (id)copy { - var objects = [], - object = nil, - objectEnumerator = [self objectEnumerator]; - - while ((object = [objectEnumerator nextObject]) !== nil) - objects.push(object); - - return [[CPSet alloc] initWithArray:objects]; + // We do this to ensure we return a CPSet. + return [CPSet setWithSet:self]; } @end diff --git a/Foundation/CPSet/CPSet.j b/Foundation/CPSet/CPSet.j index db19b8d56..1ae1fa5bc 100644 --- a/Foundation/CPSet/CPSet.j +++ b/Foundation/CPSet/CPSet.j @@ -383,7 +383,7 @@ - (id)copy { - return [[CPSet alloc] initWithSet:self]; + return [[self class] setWithSet:self]; } - (id)mutableCopy