簡體   English   中英

聲明數組時“初始化元素不是恆定的”

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

這就是我聲明數組的方式:

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

但我得到:

初始化器元素不是恆定的

那么,聲明靜態數組的最佳方法是什么?

您想要:

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

要么:

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

編輯然而,真正的問題是,你不能初始化靜態數組是這樣的。 您必須執行以下操作:

static NSArray * atouts = nil;

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

您確定在該行中收到該錯誤嗎? 因為錯誤是關於C數組的,所以AFAIK。

無論如何,您需要使用[[NSArray alloc] initWithObjects:...]或[NSArray arrayWithObjects:...]來代替[[NSArray alloc] arrayWithObjects:...]。 請注意,后者是自動發布的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM