简体   繁体   中英

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

这是因为您被称为返回数据的方法(+ arrayWithObjects)-尽管结果是不变的,但它实际上是动态生成的数据。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM