在 sprite kit Xcode 中创建整数数组
Creating an integer array in sprite kit Xcode
关于这个主题我真的没什么好说的,因为我找不到任何关于它的内容。我只想要一个整数数组,我可以在我的 iOS 游戏中参考它们的物品价格。例如
Array priceArray = Array(50);
itemAPrice = Array (0);
itemBPrice = Array (1);
我知道它效率不高,但这纯粹是一个例子。
任何有关在 Sprite 工具包中创建 integer/NSInteger 数组的内容都会有所帮助。
提前致谢
-瑞安
你遇到过 NSMutableArray 吗?您可以使用它来初始化数组
NSMutableArray *array = [NSMutableArray createWithObjects: /*comma separated list of int's*/];
使用 removeLastElement
和 addElements
方法可以更快地添加和删除元素,因为它们不必对数组重新排序。但是如果你需要,你可以使用 insertObject: /*object*/ atIndex: /*int*/
和 removeObjectAtIndex: /*int*/
.
还有 NSMutableArray,我相信,就像 NSArray 一样,您可以使用方括号表示法来引用元素,即 array[i]
= /element at int i/ .
NSArray
在 Objective-C 中是不可变的。您应该使用 NSMutableArray
它是 NSArray
:
的子类
// Create the array. Capacity is only a suggestion, not a hard limit
NSMutableArray * priceArray = [NSMutableArray arrayWithCapacity:50];
// You can't add doubles directly to the array. Wrap it inside NSNumber
[priceArray addObject:@0.0];
[priceArray addObject:@1.0];
// ...
[priceArray addObject:@49.0];
// Now get it back
double itemAPrice = [priceArray[0] doubleValue];
double itemBPrice = [priceArray[1] doubleValue];
你可以像这样使用 NSNumbers 来做到这一点:
typedef NS_ENUM(NSInteger, ItemPrice) {
ItemOne = 0,
ItemTwo,
ItemThree,
ItemFour
};
// Make it mutable so we can add more prices later if we want.
NSMutableArray *itemPrices = [@[@12, @33, @26, @44] mutableCopy];
NSNumber *itemPrice = [itemPrices objectAtIndex:ItemThree];
NSLog(@"Item Price: %@", itemPrice);
将导致:
Item Price: 2
关于这个主题我真的没什么好说的,因为我找不到任何关于它的内容。我只想要一个整数数组,我可以在我的 iOS 游戏中参考它们的物品价格。例如
Array priceArray = Array(50);
itemAPrice = Array (0);
itemBPrice = Array (1);
我知道它效率不高,但这纯粹是一个例子。 任何有关在 Sprite 工具包中创建 integer/NSInteger 数组的内容都会有所帮助。
提前致谢 -瑞安
你遇到过 NSMutableArray 吗?您可以使用它来初始化数组
NSMutableArray *array = [NSMutableArray createWithObjects: /*comma separated list of int's*/];
使用 removeLastElement
和 addElements
方法可以更快地添加和删除元素,因为它们不必对数组重新排序。但是如果你需要,你可以使用 insertObject: /*object*/ atIndex: /*int*/
和 removeObjectAtIndex: /*int*/
.
还有 NSMutableArray,我相信,就像 NSArray 一样,您可以使用方括号表示法来引用元素,即 array[i]
= /element at int i/ .
NSArray
在 Objective-C 中是不可变的。您应该使用 NSMutableArray
它是 NSArray
:
// Create the array. Capacity is only a suggestion, not a hard limit
NSMutableArray * priceArray = [NSMutableArray arrayWithCapacity:50];
// You can't add doubles directly to the array. Wrap it inside NSNumber
[priceArray addObject:@0.0];
[priceArray addObject:@1.0];
// ...
[priceArray addObject:@49.0];
// Now get it back
double itemAPrice = [priceArray[0] doubleValue];
double itemBPrice = [priceArray[1] doubleValue];
你可以像这样使用 NSNumbers 来做到这一点:
typedef NS_ENUM(NSInteger, ItemPrice) {
ItemOne = 0,
ItemTwo,
ItemThree,
ItemFour
};
// Make it mutable so we can add more prices later if we want.
NSMutableArray *itemPrices = [@[@12, @33, @26, @44] mutableCopy];
NSNumber *itemPrice = [itemPrices objectAtIndex:ItemThree];
NSLog(@"Item Price: %@", itemPrice);
将导致:
Item Price: 2