开发者

initializer not constant?

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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜