简体   繁体   中英

“initializer element is not constant” when declaring an array

This is how I declare my array :

NSArray *atouts = [[NSArray alloc] arrayWithObjects:@"1",@"2",nil];

but I'm getting :

Initializer element is not constant

What would be the best way to declare a static array then ?

You want either:

NSArray * atouts = [[NSArray alloc] initWithObjects:@"1", @"2", nil];

Or:

NSArray * atouts = [NSArray arrayWithObjects:@"1", @"2", nil];

edit however, the real problem is that you can't initialize a static array like this. You have to do something like:

static NSArray * atouts = nil;

//in some method that's invoked early
atouts = [[NSArray alloc] initWithObjects:@"1", @"2", nil];

Are you sure you get that error in that line ? Because the error is about C arrays, AFAIK.

Anyway, instead of [[NSArray alloc] arrayWithObjects:...] you need to use either [[NSArray alloc] initWithObjects:...] or [NSArray arrayWithObjects:...]. Note that the later is autoreleased.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM