Finger twist
Finger twist

Reputation: 3786

"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 ?

Upvotes: 1

Views: 1670

Answers (2)

Dave DeLong
Dave DeLong

Reputation: 243146

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];

Upvotes: 3

DarkDust
DarkDust

Reputation: 92336

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.

Upvotes: 0

Related Questions