Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!)
Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!)
- Subject: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!)
- From: Alastair Houghton <email@hidden>
- Date: Thu, 3 Jun 2004 14:49:42 +0100
On 3 Jun 2004, at 14:05, Ondra Cada wrote:
>
Hi all,
>
>
just bumped into this: mutable arrays do *NOT* deep copy upon receiving
>
-copy
>
>
#import <Foundation/Foundation.h>
>
int main() {
>
[NSAutoreleasePool new];
>
id a=[NSMutableArray arrayWithObject:[NSMutableArray array]],b=[a
>
copy];
>
NSLog(@"contents: %x %x",[a lastObject],[b lastObject]);
>
// this is why it is very very bad
>
[[a lastObject] addObject:@"this is WAY wrong if seen in b!!!"];
>
NSLog(@"changed a, my private b is %@",[b description]);
>
return 0;
>
}
>
>
Didn't check mutable sets, dicts, et cetera, but i'd bet they share
>
this howler :(((((
Are you certain that this is a bug? I don't see where in the
documentation for NSCopying it says that the copy is necessarily a deep
one, and it seems to make more sense in most cases for a collection
class's -copy to perform a shallow copy, especially for the very common
case where a collection is used to hold a set of immutable objects.
I actually think that it would be a bug if it performed a deep copy,
although it would be useful if there were a method that could be used
to perform deep copies for the few cases where that is what you really
want.
Kind regards,
Alastair.
--
http://www.alastairs-place.net
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.