Re: Initializing an Array
Re: Initializing an Array
- Subject: Re: Initializing an Array
- From: Simon Stapleton <email@hidden>
- Date: Tue, 22 Jan 2002 10:54:37 +0000 (GMT)
>
From: Andy <email@hidden>
>
Reply-To: email@hidden
>
To: cocoa-dev <email@hidden>
>
Subject: Re: Initializing an Array
>
>
"John C. Randolph" wrote:
>
>
>
> On Monday, January 21, 2002, at 02:18 PM, Michael P. Rogers wrote:
>
>
>
> > Is it possible to initialize an instance variable array in
>
> > Objective-C? PB doesn't like it when I attempt to do so in the
>
> > interface section, i.e., code like this fails:
>
> >
>
> > NSString * sayings[2] = {@"Do unto",@"others"};
>
>
>
> Try
>
>
>
> NSArray *myArray = [[NSArray arrayWithObjects:@"Do
>
> unto",@"others", nil] retain];
>
>
>
> in your +initialize method instead.
>
>
>
>
I found this last night, and its certainly a good replacement for
>
the similar Java construct. I just wish it were better
>
documented... does this 'nil' end up in the NSArray after
>
construction, ie is the length of the above 2 or 3?
NSArray can't store nil. The nil performs the same purpose as the
terminating null on a 'C' string. And yes, if you want to store
a 'nil' type object in an array, you can do that too. Look into
NSNull.
Remember, this method is a convenience constructor, so the resulting
array has already been autoreleased. Don't forget to retain it if
you need to keep it around.
As an aside, the documentation of this is pretty explicit, easy to
find, and even gives an example. I don't see how it could be much
better, frankly.
The concepts behind the collection classes are also very well
documented - Look into Programming Topics / Data Management /
Collections in your installed documentation.
Simon
--
PGP Key ID : 0x50D0698D
--
If the answer isn't obvious, the question is a distraction. Go find
an easier question.