简体   繁体   English

声明数组时“初始化元素不是恒定的”

[英]“initializer element is not constant” when declaring an array

This is how I declare my array : 这就是我声明数组的方式:

NSArray *atouts = [[NSArray alloc] arrayWithObjects:@"1",@"2",nil];

but I'm getting : 但我得到:

Initializer element is not constant 初始化器元素不是恒定的

What would be the best way to declare a static array then ? 那么,声明静态数组的最佳方法是什么?

You want either: 您想要:

NSArray * atouts = [[NSArray alloc] initWithObjects:@"1", @"2", nil];

Or: 要么:

NSArray * atouts = [NSArray arrayWithObjects:@"1", @"2", nil];

edit however, the real problem is that you can't initialize a static array like this. 编辑然而,真正的问题是,你不能初始化静态数组是这样的。 You have to do something like: 您必须执行以下操作:

static NSArray * atouts = nil;

//in some method that's invoked early
atouts = [[NSArray alloc] initWithObjects:@"1", @"2", nil];

Are you sure you get that error in that line ? 您确定在该行中收到该错误吗? Because the error is about C arrays, AFAIK. 因为错误是关于C数组的,所以AFAIK。

Anyway, instead of [[NSArray alloc] arrayWithObjects:...] you need to use either [[NSArray alloc] initWithObjects:...] or [NSArray arrayWithObjects:...]. 无论如何,您需要使用[[NSArray alloc] initWithObjects:...]或[NSArray arrayWithObjects:...]来代替[[NSArray alloc] arrayWithObjects:...]。 Note that the later is autoreleased. 请注意,后者是自动发布的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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