Re: Initializing an Array
Re: Initializing an Array
- Subject: Re: Initializing an Array
- From: "John C. Randolph" <email@hidden>
- Date: Tue, 22 Jan 2002 11:02:16 -0800
On Monday, January 21, 2002, at 07:46 PM, Andy wrote:
"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?
[myArray count] will return 2.
-arrayWithObjects: is a varargs method, and the nil is the list
terminator.
-jcr
John C. Randolph <email@hidden> (408) 974-8819
Sr. Software Engineer, Cocoa Evangelism
Apple Worldwide Developer Relations