Friday, April 15, 2011

Initializer Element Is Not Constant


This code might look valid to less experienced iOS developers, but it does give a compiler error "initializer element is not constant":

static NSArray *playState = [NSArray arrayWithObjects:
    @"MPMoviePlaybackStateStopped",
    @"MPMoviePlaybackStatePlaying",
    @"MPMoviePlaybackStatePaused",
    @"MPMoviePlaybackStateInterrupted",
    @"MPMoviePlaybackStateSeekingForward",
    @"MPMoviePlaybackStateSeekingBackward",
    nil];
The problem is that variable playState is a constant, whose value depends on other variable, whose value is not known. Seems like Objective-C standard does not allow that kind of initialization setup

Fix it like this:
static NSArray *playState = nil;
playState = [NSArray arrayWithObjects:
    @"MPMoviePlaybackStateStopped",
    @"MPMoviePlaybackStatePlaying",
    @"MPMoviePlaybackStatePaused",
    @"MPMoviePlaybackStateInterrupted",
    @"MPMoviePlaybackStateSeekingForward",
    @"MPMoviePlaybackStateSeekingBackward",
    nil];
Congratulations! Welcome to the selected group of more experienced iOS developers!

Btw that was only about the compiler warning, nothing else! If you want to know how to use static variables, please check "Local Static Variable Lifetime Expectation in Objective-C".

No comments:

Post a Comment