• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Why use copy in value setters?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why use copy in value setters?


  • Subject: Re: Why use copy in value setters?
  • From: Jim Hamilton <email@hidden>
  • Date: Mon, 22 Aug 2005 12:02:19 -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?

Here's one reason:

Assume an instance variable that's an NSString, and the following setter:

- (void)setString:(NSString *)inString {
    [string autorelease];
    string = [inString retain];
}

Now, set it, in the instance foo:

NSMutableString* bar;

// something sets bar

[foo setString:bar];

// something appends to bar

Boom, you've just changed the ivar's value.  Did you mean to?

Jim H
--
Jim Hamilton

email@hidden
email@hidden

_______________________________________________
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


References: 
 >Why use copy in value setters? (From: Larry Gerndt <email@hidden>)

  • Prev by Date: Re: Clearing NSTableView on unbind
  • Next by Date: Re: Core Data and undo action names
  • Previous by thread: Why use copy in value setters?
  • Next by thread: Re: Why use copy in value setters?
  • Index(es):
    • Date
    • Thread