int[] 的 NSArray [英] NSArray of int[]

查看:15
本文介绍了int[] 的 NSArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我的关卡存储一堆 int[].

I am needing to store a heap of int[] for my levels.

我决定将 int[] 存储在 NSMutableArray 中并从数组中随机获取一个将是一个很好的方法.

I decided that storing the int[]'s in an NSMutableArray and getting a random one from the array would be a good way to do it.

事实是,一个 int[] 不是一个对象,你不能将它添加到一个 obj-c 数组中.

Thing is, an int[] is not an object and you cannot add it inside an obj-c array.

有人对我如何获得随机整数数组有任何建议吗?

Does anyone have a suggestion on how I can get a random integer array?

我的数组如下所示:

int lvl1[] { 4,80,49,6,40,77,21,20,91,5,100,91,...... }; 
int lvl2[] { 20,260,385,20,290,448,21,210,329,21,...... }; 
int lvl3[] { 441,21,90,364,21,70,385,21,170,434,...... }; 
...
int lvl50[] { 441,21,90,364,21,70,385,21,170,434,...... }; 

然后我需要随机获得一个.

I then need to get a random one of these.

推荐答案

改为使用一种不太邪恶的方法,由 Tommy 的评论提供.

changed to use a less evil method courtesy of Tommy's comment.

您可以将静态数组视为指针并将它们存储在 NSValue 对象中:

You can treat the static arrays as pointers and store them in NSValue objects:

[mutableArray addObject:[NSValue valueWithPointer:lvl1]];

...

int* level = [(NSValue*)[mutableArray objectAtIndex:whatever] pointerValue];
int someContainedInt = level[index];

或者,您可以将每个单独的数组包装在其自己的 NSData 对象中,并将它们存储在可变数组中:

Alternatively, you could wrap each individual array in its own NSData object and store those in the mutable array:

[mutableArray addObject:[NSData dataWithBytes:lvl1 length:sizeof(int) * lengthOfArray]];

...

const int* level = (const int*) [(NSData*) [mutableArray objectAtIndex:whatever] bytes];

我不得不同意 Frank C 的观点——为什么你需要使用 Cocoa 数组来存储这些数组?你不能把整个地块当作一个二维 C 数组吗?如果它是静态数据,那么 NSMutableArray 的动态方面似乎是多余的.

I have to concur with Frank C, though -- why do you need to use a Cocoa array to store these arrays at all? Can't you just treat the whole lot as a 2D C array? If it's static data anyway then the dynamic aspects of NSMutableArray seem pretty much superfluous.

编辑 2: 使用 C 数组

如果你的关卡数组的长度都相同——称之为LEVEL_SIZE——你可以像这样构建一个直的二维数组:

If your level arrays are all the same length -- call it LEVEL_SIZE -- you can build a straight 2D array like this:

static int levels[][LEVEL_SIZE] = 
{
    {1, 2, 3, 4, ...},
    {15, 17, 19, 21, ...},
    {8, 7, 6, 5, ...}
};

否则,您需要分别构建每个数组,然后再将它们组合在一起:

Otherwise, you'll need to build each array separately and then put them together afterwards:

static int level1[] = {1, 2, 3, 4, ...};
static int level2[] = {15, 17, 19, 21, ...};
static int level3[] = {8, 7, 6, 5, ...};
...
static int* levels[] = {lvl1, lvl2, lvl3, ...};

无论哪种方式,您都可以提取一个级别作为指针:

Either way, you can pluck out one level as a pointer:

int* level = levels[0];
printf("%d\n", level[1]); // should print 2

首先,您将有 NUM_LEVELS 个级别——在您的情况下为 50——因此将许多索引 0..49 粘贴到您的可变数组中,如 NSNumber 对象:

To start with, you'll have NUM_LEVELS levels -- in your case 50 -- so stick that many indices 0..49 into your mutable array, as NSNumber objects:

NSMutableArray* levelIndices = [NSMutableArray arrayWithCapacity:NUM_LEVELS];
for ( int i = 0; i < NUM_LEVELS; ++i )
    [levelIndices addObject:[NSNumber numberWithInt:i]];

使用此数组来满足您的计数、获取和删除需求.当你拉出一个 NSNumber 对象时,用它来索引到 levels 数组以获得级别:

Use this array for your counting, getting and removing needs. When you pull an NSNumber object out, use it to index into the levels array to get the level:

int* level = levels[[someNSNumber intValue]];

等等.

这篇关于int[] 的 NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆