RE: Trying to understand -- please help...
RE: Trying to understand -- please help...
- Subject: RE: Trying to understand -- please help...
- From: Caleb Strockbine <email@hidden>
- Date: Thu, 22 May 2008 10:27:43 -0400
On May 21, 2008, at 2:39 PM, Johnny Lundy <email@hidden> wrote:
Here's why the OP was not aware of the behavior of an NSArray class
method:
Here's the verbatim documentation for +arrayWithObjects:
[...deleted...]
See? Not a word about autoreleasing anything, or having to retain the
returned array. Nada.
You don't have to retain the returned array. You can just create it,
use it,
and then forget about it. You DO have to retain the returned array
*if* you
want to keep a reference to it, but that's true of *any* object you
create
with a convenience constructor, or indeed any object that you didn't
create
yourself using alloc, new, or copy.
I agree that there's some room for confusion if the documentation for
NSArray is the first thing you read about Cocoa, and then you try to
jump
in and start coding. However, the basics of retain counts and object
ownership are explained in the first article of the first guide
listed under
Cocoa Fundamentals. That would be "Memory Management Programming
Guide for Cocoa"->"Object Ownership and Disposal". It's explained again,
with examples, in the second article, "Practical Memory Management."
It's clear from the OP's post that he's still just beginning to learn
about
Cocoa. For example, it seems that he's trying to initialize an array and
then add some objects to it in his code, and he doesn't understand
that instances of NSArray can't be changed after they're created.
Although
folks here corrected his code by removing the [[NSArray alloc] init]
line
and adding a retain, it might have been a greater service to him to
change
the first line to [[NSMutableArray alloc] init] and changed the
subsequent
line to call -addObject: rather than +arrayWithObjects.
In any case, the important point here is not that the documentation for
NSArray or memory management is lacking, but that it appears that the
OP hasn't yet read either the most fundamental documents or even the
NSArray reference page.
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."
The NSArray documentation describes NSArray quite completely in the
context of a Cocoa environment, which is to say that it follows the
rules
that every other class does, and it documents any particulars that you
might need to know (e.g. NSArray retains the objects it contains for
the life of the array). I'm sure that there are ways that the docs could
be improved, but IMO, adding "don't forget to retain the returned
object" to the description for every method in every class in Cocoa that
returns an object would not be an improvement.
regards,
Caleb Strockbine
_______________________________________________
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