• 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: mmalcolm crawford <email@hidden>
  • Date: Tue, 16 Dec 2003 13:45:13 -0800

On Dec 16, 2003, at 11:36 AM, Lawrence Meeran wrote:
i'm no expert but i know the answer to this one...

Umm, not quite.

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.

I'm not sure quite what you mean by "something clever". There are a number of reasons why you might not follow that pattern:
<http://www.stepwise.com/Articles/Technical/2002-06-11.01.html>

In this case in particular, since (a) the array being passed in is mutable and (b) to preserve encapsulation you probably want to ensure the object maintains its own variables, the most likely implementation will be something more like:

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



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.

Think about what happens is inArray == array, and you're the only object retaining it prior to calling the method...

if you want a copy, i would GUESS you do:
array = [[inArray mutableCopy] retain];

No. As is I suspect stated by all the documentation on the subject, copy methods return objects with a retain count of 1, not autoreleased. There is no need for the retain.

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

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

  • Prev by Date: Required InfoPlist entries
  • Next by Date: Re: Freelance programming
  • Previous by thread: Re: retain variables between classes
  • Next by thread: Re: retain variables between classes
  • Index(es):
    • Date
    • Thread