views:

68

answers:

3

Quick question if I may: I am just curious about the following (see below) Xcode says "initializer element is not constant" why this does not work, I guess its the NSArray ...

static NSArray *stuffyNames = [NSArray arrayWithObjects:@"Ted",@"Dog",@"Snosa",nil];

and this does ...

static NSString *stuffyNames[3] = {@"Ted",@"Dog",@"Snosa"};

gary

+3  A: 

Its because you are called a method (+ arrayWithObjects) that returns data - although the result is immutable, its actually dynamically generated data.

Andiih
+2  A: 

Static local variables are initialized at compile time so their initializer must also be known at compile time, which is obviously not true in you 1st example.

Static variables may be initialized in their declarations; however, the initializers must be constant expressions, and initialization is done only once at compile time when memory is allocated for the static variable.

and more on static variables.

Vladimir
+1  A: 

Yes, it's the NSArray. Think about what happens at compile time.

In the second case it has all the information it needs. It has three NSString constants and a C-style array to put them in.

On your first line you have a call to a class method with four parameters, all of which happen to be constants. As far as the compiler is concerned, NSArray is no different from, say, UIApplication. It's a class with paramters. You and I know that it's an array but the implementation of that is in the Foundation library and not a core part of the language.

Stephen Darlington