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



0
0
1
106
610
Oak Leaf Enterprises, Inc
5
1
715
14.0







Normal
0




false
false
false

EN-US
JA
X-NONE









































































































































































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”

                             @”iPhone”,

                             @”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 method:

- (void)viewDidLoad

{

    [super viewDidLoad];

     

      devices = [[NSArray alloc]

         initWithObjects:@”iPod Touch”

         @”iPhone”,

         @”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
http://www.iOSAppsForEveryone.com 

 

 


 

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>