Re: Why use copy in value setters?
Re: Why use copy in value setters?
- Subject: Re: Why use copy in value setters?
- From: Jim Correia <email@hidden>
- Date: Mon, 22 Aug 2005 12:12:34 -0400
On Aug 22, 2005, at 11:51 AM, Larry Gerndt wrote:
Apple recommends that setters which provide "values" as opposed to
"entities" should copy the value object instead of retain it.
Quoting Apple:
"The general rule is this: When the object assigned to an instance
variable is a value object—that is, an object that represents some
attribute such as a string, date, number, or corporate record—you
should copy it. Otherwise, it’s an entity object such as an NSView
or an NSWindow and you should retain it."
My question is simply why?
Because you are interested in the value, not the entity, and need to
take a snapshot of it.
Here's what can happen if you do not.
NSMutableString *string = [NSMutableString stringWithString: @"Fred];
[person setFirstName: string];
[string appendString: @" Flintstone"];
NSLog("%@", [person firstName]);
--> Fred Flintstone
Jim
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden