Dan*_*iel 4 java enums objective-c
我在Objective-C中有以下枚举:
typedef enum {
APIErrorOne = 1,
APIErrorTwo,
APIErrorThree,
APIErrorFour
} APIErrorCode;
Run Code Online (Sandbox Code Playgroud)
我使用索引来引用xml中的枚举,例如,xml可能有error = 2,映射到APIErrorTwo
我的流程是从xml获取一个整数,并运行switch语句,如下所示:
int errorCode = 3
switch(errorCode){
case APIErrorOne:
//
break;
[...]
}
Run Code Online (Sandbox Code Playgroud)
似乎Java在switch语句中不喜欢这种枚举:

在Java中,似乎无法为enum成员分配索引.我怎样才能获得与上述相同的Java?
Java枚举有一个内置序号,第一个枚举成员为0,第二个为1,等等.
但枚举是Java中的类,因此您也可以为它们分配一个字段:
enum APIErrorCode {
APIErrorOne(1),
APIErrorTwo(27),
APIErrorThree(42),
APIErrorFour(54);
private int code;
private APIErrorCode(int code) {
this.code = code;
}
public int getCode() {
return this.code;
}
}
Run Code Online (Sandbox Code Playgroud)