1

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 ?

2 Answers 2

3

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];
Sign up to request clarification or add additional context in comments.

2 Comments

hmm, still getting it using any of those 2 lines, could it be related to my header file ? ( NSArray *atouts; )
thanks for looking into it, however it's now erroring like this : "warning: data definition has no type or storage class" - "warning: type defaults to 'int' in declaration of 'atouts'" - " error: conflicting types for 'atouts'"
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.