Lollipopassasin
Lollipopassasin

Reputation: 21

Objective C Initialiser element is not a compile-time constant

I am getting the error 'Initialiser element is not a compile-time constant' when I attempt to run my build. It is shown that the Array is the source of the problem. My code is as follows:

@implementation XYZViewController 
//Array setup 
NSArray *example = @[@"X", @"Y", @"Z"];

@end

This is not all of my code but I as of currently don't find it a necessity to have my complete code shown here.

Upvotes: 2

Views: 3639

Answers (2)

Gabriele Petronella
Gabriele Petronella

Reputation: 108101

The point is that @[@"X", @"Y", @"Z"]; is not a static initializer, since the compiler translates it to a method call to arrayWithObjects:count: of NSArray.

A static initialized cannot be a method call and of course the compiler complains about it.

If you want to inizialize your array you can do it inside a method at runtime. You have several options for doing that.

If you want to inizialize the array for every instance, just do that inside the default inizializer (which one is it depends on the specific class).

If you want to do so at a class level, you can do it inside the initialize class method.

Upvotes: 2

Balu
Balu

Reputation: 8460

you are trying to declare the array in this block that's why it shows an error, try like this ,

@implementation XYZViewController {
    //Array setup 
    NSArray *example ;
}

@end

assign the array values in viewDidLoad method or some where elselike this example = @[@"X", @"Y", @"Z"];.

Upvotes: 1

Related Questions