Trying to understand -- please help...
Trying to understand -- please help...
- Subject: Trying to understand -- please help...
- From: Johnny Lundy <email@hidden>
- Date: Wed, 21 May 2008 14:35:55 -0400
Here's why the OP was not aware of the behavior of an NSArray class
method:
Here's the verbatim documentation for +arrayWithObjects:
arrayWithObjects:
Creates and returns an array containing the objects in the argument
list.
+ (id)arrayWithObjects:(id)firstObj, ...
ParametersfirstObj, ...
A comma-separated list of objects ending with nil.
Return Value
An array containing the objects in the argument list.
Discussion
This code example creates an array containing three different types
of element:
NSArray *myArray;
NSDate *aDate = [NSDate distantFuture];
NSValue *aValue = [NSNumber numberWithInt:5];
NSString *aString = @"a string";
myArray = [NSArray arrayWithObjects:aDate, aValue, aString, nil];
Availability
• Available in Mac OS X v10.0 and later.
See Also
• + array
• + arrayWithObject:
Declared InNSArray.h
See? Not a word about autoreleasing anything, or having to retain the
returned array. Nada.
To make it even more demonstrative of what I have been saying about
the documentation, here's the excerpt of the "Overview" section in
NSArray Class Reference dealing with retain-release of NSArrays:
Generally, you instantiate an array by sending one of the array...
messages to either the NSArray orNSMutableArray class object. These
methods return an array containing the elements you pass in as
arguments. (Note that arrays can’t contain nil.) In general, objects
that you add to an array aren’t copied; rather, each object receives
a retain message before its id is added to the array. When an object
is removed from an array, it’s sent a release message.
It says each element is sent a retain message when added, and **is
sent a release message when REMOVED from the array***.
Again, nothing about autorelease. And he sure isn't removing anything
from the array.
So naturally, the OP is going to believe that if he created an array,
for the purpose of initializing one of his objects, that it will be
available for later use. What other purpose would there be for
initializing something? What's the purpose of an -init method anyway,
if not to load something with data to be used later?
I just want everyone who "knows" this to see what a programmer sees
when turning to the page that supposedly describes the API object that
he wants to learn how to use.
Seriously, read that assuming you wanted to try out the NSArray class
and tell me how it accomplishes its purpose of documenting what it is
intended to document. It doesn't. And this pattern is repeated over
and over in just about every one of the "Class References."
All it would take is a single sentence under the "Class Methods"
header, to say that these methods autorelease the returned object (AND
that such autoreleasing means that the object will be deallocated if
the method in which it was created returns). That is boilerplate, and
would have to be copied to all 250 Class Reference pages, but hey,
they copy and paste a lot less important boilerplate than that.
On May 21, 2008, at 1:53 PM, Peter Hudson wrote:
Message: 2
Date: Wed, 21 May 2008 17:47:38 +0100
From: Peter Hudson <email@hidden>
Subject: Trying to understand -- please help...
To: email@hidden
Cc: email@hidden
Message-ID: <email@hidden>
Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed
The line of code [NSArray arrayWithObjects: c0, ...c9, nil];
produces
an array all right - but as you have used a class initializer, it
will auto release
when you return from the method where it was created.
Any initialiser with a "+" in front of it returns an autoreleased
object
- hence you MUST retain them or else they are simply released
when the autorelease pool they are created in is released - which you
can treat
as occurring at the end of the method in which the init occurs.
Do it like this [ [NSArray arrayWithObjects: c0, ...c9, nil]
retain];
and then it will stay around until you send it a release message,
probably in the dealloc method;
[cityArray release];
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden