• 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: retain variables between classes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: retain variables between classes


  • Subject: Re: retain variables between classes
  • From: Shawn Erickson <email@hidden>
  • Date: Tue, 16 Dec 2003 14:15:29 -0800

On Dec 16, 2003, at 11:36 AM, Lawrence Meeran wrote:

hello

i'm no expert but i know the answer to this one...
unless you're trying to do something clever, the 'canonical' way of doing it is like this:

- (void) setInfo: (NSMutableArray*) inArray
{
NSArray* a = array;
array = [inArray retain];
[a release];
}

which will make array *exactly the SAME object* you passed in - ie not an independent copy.
actually i've seen people on this list saying you can just do this:

- (void) setInfo: (NSMutableArray*) inArray
{
[array release];
array = [inArray retain];
}

but i'm not sure if this is 'safe' -- if it isn't, i'd like to know why not.
if you want a copy, i would GUESS you do:

The Apple recommended pattern for a setter is the following.

- (void) setInfo: (NSMutableArray*) inArray
{
if (array != inArray) {
[array release];
array = [inArray retain]; or [inArray copy]; or [inArray mutableCopy];
}
}

...
array = [[inArray mutableCopy] retain];
...

so i think all you're doing wrong, really, is not retaining the object.

You do not need to retain a copied object. When you copy an object you are the one allocating it, so it already has an implicit retain (just as if you used alloc). So you have to be the one to make sure the implicit retain is balance by a release.

-Shawn
_______________________________________________
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.

References: 
 >Re: retain variables between classes (From: Lawrence Meeran <email@hidden>)

  • Prev by Date: Re: sending mail
  • Next by Date: Re: Bug when encoding subclasses of NSData?
  • Previous by thread: Re: declaring variables to be const (was retain variables between classes)
  • Next by thread: RE: retain variables between classes
  • Index(es):
    • Date
    • Thread