Re: retain variables between classes
Re: retain variables between classes
- Subject: Re: retain variables between classes
- From: Lawrence Meeran <email@hidden>
- Date: Tue, 16 Dec 2003 23:30:29 +0000
On Tuesday, December 16, 2003, at 09:45 PM, mmalcolm crawford wrote:
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.
oh...
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];
}
this is what i meant by "something clever" -- where it would be wrong
to just retain the object passed in.
i (think i) understand the encapsulation issue; it is obviously
something to be wary of. But aren't there cases where you might not
really need or want to copy the object? isn't there an extra overhead
involved with all the alloc/deallocation?
i'm less clear about the issue with the mutability of the array; in
this particular case (just setting an instance var) would it not make
sense to declare the argument as NSArray rather than NSMutableArray?
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...
ok, i get that now.
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.
ok. so far i've not used the copy methods because i've been wanting to
keep references to the actual objects - hence i haven't read the copy
docs closely. unnecessary overheads are my main worry at the mo. i've
only been playing with cocoa for a couple of months - as i said, i'm no
expert.
thanks for enlightening me
L
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.
_______________________________________________
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.