Understanding iOS Compiler Warnings – Initializer Element Is Not a Compile-Time Constant

Initializer Element Is Not a Compile-Time Constant

You usually get this compiler error if you try to assign a
non-constant value to an instance variable. For example, you will get this
error if you place the following code outside of a class method:

NSArray *devices
= [[NSArray alloc]

                             initWithObjects:@”iPod Touch”


                             @”iPad”, nil];

Although you can do this in other languages, you can’t do it in Objective-C.

To fix the problem, declare the instance variable without
initializing it:

NSArray *devices;

Then initialize the instance variable elsewhere, such as in
a view controller’s viewDidLoad

– (void)viewDidLoad


    [super viewDidLoad];

      devices = [[NSArray

         initWithObjects:@”iPod Touch”


         @”iPad”, nil];


For explanations of other compiler errors and warnings, check out our new book site: http://www.iOSAppsForEveryone.com

Kevin McNeish
Eight-time .NET MVP Recipient
Apple iOS Author, Trainer


Leave a Reply

Your email address will not be published. Required fields are marked *