Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8246331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:28:17+00:00 2026-06-07T22:28:17+00:00

I have the following enum in Objective-C: typedef enum { APIErrorOne = 1, APIErrorTwo,

  • 0

I have the following enum in Objective-C:

typedef enum {
    APIErrorOne = 1,
    APIErrorTwo,
    APIErrorThree,
    APIErrorFour
} APIErrorCode;

I use the indexes to reference an enum from an xml, for example, xml may have error = 2, which maps to APIErrorTwo

My flow is I get an integer from the xml, and run a switch statement as follows:

int errorCode = 3

switch(errorCode){
    case APIErrorOne:
        //
        break;
    [...]
}

Seems Java dislikes this kind of enum in a switch statement:

enter image description here

In Java it seems you can’t assign indexes to enum members. How can I get a Java equivalent of the above ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:28:19+00:00Added an answer on June 7, 2026 at 10:28 pm

    One question per post is the general rule here.

    But evolving the JB Nizer answer.

    public enum APIErrorCode {
    
        APIErrorOne(1),
        APIErrorTwo(27),
        APIErrorThree(42),
        APIErrorFour(54);
    
        private final int code;
    
        private APIErrorCode(int code) {
            this.code = code;
        }
    
        public int getCode() {
            return this.code;
        }
    
        public static APIErrorCode getAPIErrorCodeByCode(int error) {
           if(Util.errorMap.containsKey(error)) {
             return  Util.errorMap.get(error);
           }
           //Or create some default code
           throw new IllegalStateException("Error code not found, code:" + error);
        }
    
        //We need a inner class because enum are  initialized even before static block
        private static class Util {
    
            private static final Map<Integer,APIErrorCode> errorMap = new HashMap<Integer,APIErrorCode>();
    
            static {
    
                for(APIErrorCode code : APIErrorCode.values()){
                    errorMap.put(code.getCode(), code);
                }
            }
    
        }
    }
    

    Then in your code you can write

    int errorCode = 3
    
    switch(APIErrorCode.getAPIErrorCodeByCode(errorCode){
        case APIErrorOne:
            //
            break;
        [...]
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following: typedef enum functionType {ln, sin, sqrt} functionType; NSArray *functions
I have an Objective-C protocol: typedef enum { ViewStateNone } ViewState; @protocol ViewStateable -
I have the following struct: typedef enum { kPacketTypeJoin = 1, kPacketTypeServerAcceptedJoin, kPacketTypeSelectedNumber, }
If I have the following enum type: typedef enum {Type1=0, Type2, Type3} EnumType; And
I have the following block in my JSP, which converts from ENUM values {CREATE,
I have following code: typedef enum { FOO, BAR, BAZ } foo_t; static void
I have following Enum in my project public enum CameraAccessMethod { Manual = 0,
I have the following enum how do i map in jna ?? This enum
I have the following enum: [Flags] public enum PermissionLevel { User = 1, Administrator
I have the following enum. public enum DATABASE_TYPES { JDataStore, Access, SQLServer, H2, PostGresSQL,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.