• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Initializing an Array
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Prev by Date: Re: Making a control draw while I'm in a (tightish) loop
  • Next by Date: Re: Initializing an Array
  • Previous by thread: Re: Initializing an Array
  • Next by thread: Re: Initializing an Array
  • Index(es):
    • Date
    • Thread