Re: Initializing an Array
Re: Initializing an Array
- Subject: Re: Initializing an Array
- From: Bob Savage <email@hidden>
- Date: Mon, 21 Jan 2002 22:22:35 -0600
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];
NSArray is a good solution, but if you really want to use a C array, your
code should've worked. This compiles cleanly for me:
------------------------------------------------------------------
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString * sayings[2] = {@"Do unto",@"others"};
printf("%s\n", [sayings[0] cString]);
[pool release];
return 0;
}
------------------------------------------------------------------
the result is:
"
Do unto
testMonkey has exited with status 0.
"
NOTE: if you do use C arrays like this you will lose the benefit of
NSArray's handling of memory issues for you!
Best,
Bob