为什么在下面的代码中,我不能简单地创建一个 NSNumbers 的静态数组?我只会使用 C 数组和整数,但它们无法复制,正如您在 init() 中看到的那样,我必须将数组复制到另一个数组。我收到的错误是“Initializer element is not constant”。这非常令人困惑;考虑到我在那里没有 const 关键字,我什至不确定这意味着什么。
此外,作为旁注,getNextIngredient 方法给了我错误“不能使用对象作为方法的参数”和“不兼容的类型作为回报”,但我不知道为什么。
这是代码:
// 1 = TOMATO
// 2 = LETTUCE
// 3 = CHEESE
// 4 = HAM
#import "Recipe.h"
@implementation Recipe
// List of hardcoded recipes
static NSArray *basicHam = [[NSArray alloc] initWithObjects:[[NSNumber alloc] numberwithInt:1], [[NSNumber alloc] numberwithInt:2], [[NSNumber alloc] numberWithInt:3], [[NSNumber alloc] numberwithInt:4]];
// Upon creation, check the name parameter that was passed in and set the current recipe to that particular array.
// Then, set nextIngredient to be the first ingredient of that recipe, so that Game can check it.
-(id) initWithName: (NSString*)name {
self = [super init];
indexOfNext = 0;
if (self) {
if ([name isEqualToString: @"Basic Ham"]) {
currRecipe = [NSArray arrayWithArray: basicHam];
}
}
}
-(NSNumber) getNextIngredient {
return [currRecipe objectAtIndex:indexOfNext];
}