Re: copy & isEqual nightmares
Re: copy & isEqual nightmares
- Subject: Re: copy & isEqual nightmares
- From: James Maxwell <email@hidden>
- Date: Sun, 12 Feb 2012 16:44:20 -0800
Okay, just saw an interesting post on this:
http://stackoverflow.com/questions/1459598/how-to-copy-an-object-in-objective-c
This is quite different from what I've generally seen, which just does:
MyClass* copy = [[self class] allocWithZone:zone];
then uses accessors to assign all the properties, and returns the copy.
But does that just give me a shallow copy? Do I really have to implement the version in the post -- that is, do I have to "call copyWithZone on all of [my] fields" to get a true, mutable copy, that doesn't muck with the original? I guess it makes sense, but I'm kind of surprised that the normal copy (i.e., the one I see everywhere) doesn't do that already, since that's what a "copy" seems to be, in the most general sense. (If I "copy" a paper document, I can burn the copy, write all over it, or whatever I want, without touching the original.)
J.
On 2012-02-12, at 4:25 PM, James Maxwell wrote:
> Okay, so I'm officially confused.
>
> I have an object with isEqual set up to help me limit the number of "unique" objects I can put in a Dictionary. Basically, the object has a few properties which account for "equality", and some properties that don't matter. For example, propA and propB are used in isEqual, but propC isn't. The point is that I can create a set with objects where only propA and propB are different, and propC just is irrelevant. Maybe that seems weird, but it's what I need.
>
> But what I don't get is how hash plays into all this. I've always read that I have to override hash when I override isEqual, but I don't exactly understand what that means or does. What I've done is to make a hash that is equal when two objects have the same values for propA and propB (ignoring propC). That's how I interpreted the few threads I've read about overriding hash...
>
> But what I need to do in my code, right now, is to copy an object, then alter some properties of the copy, but not the original -- a typical reason for wanting a copy. But I'm seeing that the copy has the same hash and appears to be the same object, since changing properties of the copy changes the original. So does the system see the matching hashes as identical objects, not just the collection classes? Do I have to manually implement some sort of deepCopy method? Or, can I just change hash, so that the copy is a different instance, without losing the functionality I need for collections? I don't understand why I'd need to make a deepCopy, since that's what copy is for...
>
> Any help greatly appreciated.
>
> J.
>
>
> ------------------------------------------------------
> James B. Maxwell
> Composer/Researcher/PhD Candidate
>
>
>
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
------------------------------------------------------
James B. Maxwell
Composer/Researcher/PhD Candidate
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden