次のコードで、単純に NSNumbers の静的配列を作成できないのはなぜですか? C の配列と int を使用するだけですが、これらはコピーできず、init() でわかるように、配列を別の配列にコピーする必要があります。私が受け取ったエラーは「初期化要素が定数ではありません」です。とても紛らわしいです。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];
}