Objective c 使用枚举将类型分配给一系列整数?

Objective c 使用枚举将类型分配给一系列整数?,objective-c,c,enums,Objective C,C,Enums,我有一个索引数组[1…20]。IndicateSarray的前4个元素链接到特定类型的文件(称为a型),其他16个元素链接到B型 我随机洗牌数组。我现在希望提取4个索引,但最多只能提取其中一个类型为A 我想我需要在这里使用enum函数将指数1-4定义为“A型”,将指数5-20定义为“B型”,然后,如果我查看我的新随机指数数组[0]的第一个元素,我可以判断它是哪种类型,并相应地采取行动 我从示例中看到enum的用法如下: enum category { typeA = 0, typeB }; 是

我有一个索引数组[1…20]。IndicateSarray的前4个元素链接到特定类型的文件(称为a型),其他16个元素链接到B型

我随机洗牌数组。我现在希望提取4个索引,但最多只能提取其中一个类型为A

我想我需要在这里使用enum函数将指数1-4定义为“A型”,将指数5-20定义为“B型”,然后,如果我查看我的新随机指数数组[0]的第一个元素,我可以判断它是哪种类型,并相应地采取行动

我从示例中看到enum的用法如下:

enum category { typeA = 0, typeB };
是否有可能将索引1-4分配给typeA,其余的分配给typeB,或者我在这里走错了方向?提前谢谢

编辑以包含代码段

我试图测试这一点&马上遇到了一个错误

 #import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int* indices = malloc(20*sizeof(int));

for (int i=0; i<20; i++) {

    indices[i] = i;

}

enum category {typeA, typeB};


enum category categoryForIndex(int index) {
    if (index >= 1 && index <= 4) {
        return typeA;
    } else {
        return typeB;
    }
}

[pool drain];
return 0;

}
#导入
int main(int argc,const char*argv[]{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
int*指数=malloc(20*sizeof(int));

对于(int i=0;i=1&&index=1&&index您偏离了轨道。您无法为给定的枚举分配1-4。枚举常量只有一个值,而且只有一个值。您可以使用枚举定义两种类型,例如
typeA
typeB
,然后定义一个将索引映射回类型的函数,例如

enum category categoryForIndex(int index) {
    if (index >= 1 && index <= 4) {
        return typeA;
    } else {
        return typeB;
    }
}
enum类别类别索引(int索引){

如果(index>=1&&index,则无需首先洗牌数组即可执行此操作,以便知道A始终位于前面:

#define IndexCount 20
#define ExtractCount 4
#define TypeACount 4

int indicesRemainingCount = IndexCount;
int indices[IndexCount] = { ... }; // your indices, first 4 are type A
int chosenIndices[ExtractCount]; // to be filled with random elements from indices, max one of type A

int minIndex = 0;
for (int i = 0; i < ExtractCount; ++i) {
    int j = minIndex + arc4random_uniform(indicesRemainingCount - minIndex);
    chosenIndices[i] = indices[j];
    if (j < TypeACount) {
        // Set minIndex so I won't pick another A.
        minIndex = TypeACount;
    } else {
        // Remove the chosen B so I don't pick it again.
        --indicesRemainingCount;
        indices[j] = indices[indicesRemainingCount];
    }
}
#定义索引计数20
#定义提取计数4
#定义类型帐户4
int indicatesRemainingCount=IndexCount;
int索引[IndexCount]={…};//您的索引,前4个是类型A
int ChosenIndexes[ExtractCount];//要用索引中的随机元素填充,最大值为A型
int minIndex=0;
对于(int i=0;i
C中的小心索引从
0
N-1
,包括在内。感谢pmg:)是的,我引用的数组包含索引(恰好从1开始)谢谢Kevin,这是有道理的,我尝试了它,并得到了一个我不期望的错误-与嵌套函数有关。我在上面发布了一个小代码片段。@Octave1:这是因为您试图在程序的
main
函数中定义一个函数。您需要将它移到外部(例如在
main
函数之前)。太好了,效果非常好,我发布了一些代码,展示了如何检查给定索引属于哪个类别,因为我花了一点时间才弄清楚,可能对某人有用。再次感谢dreamlax&Kevin。这是合理的,只要你愿意破坏性地修改输入数组。他说他想洗牌。所以他是要修改还是复制它。我说的“破坏性”是指你扔掉数组中的数据。你不是交换单元格,而是覆盖单元格。当然,如果你确实将其更改为交换,那么它就等同于洗牌。
#define IndexCount 20
#define ExtractCount 4
#define TypeACount 4

int indicesRemainingCount = IndexCount;
int indices[IndexCount] = { ... }; // your indices, first 4 are type A
int chosenIndices[ExtractCount]; // to be filled with random elements from indices, max one of type A

int minIndex = 0;
for (int i = 0; i < ExtractCount; ++i) {
    int j = minIndex + arc4random_uniform(indicesRemainingCount - minIndex);
    chosenIndices[i] = indices[j];
    if (j < TypeACount) {
        // Set minIndex so I won't pick another A.
        minIndex = TypeACount;
    } else {
        // Remove the chosen B so I don't pick it again.
        --indicesRemainingCount;
        indices[j] = indices[indicesRemainingCount];
    }
}