Kevin McNeish Blog

All things iOS, Kindle and .NET

Recent Posts

Tags

News

  • First books in my new book series, "iOS App Development for Non-Programmers" are now available! iBookStore: http://itunes.apple.com/us/book/book-1-diving-in-ios-app-development/id558788074?mt=11 Amazon: http://www.amazon.com/dp/B0097N8XBE Amazon: http://www.amazon.com/dp/B0099RQGMQ

Community

Email Notifications

Archives

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 Comment

(required) 

(required) 

(optional)
 

(required) 

If you can't read this number refresh your screen
Enter the numbers above: