开发者

simulate java enum objective-c

开发者 https://www.devze.com 2023-01-10 20:18 出处:网络
in java an enum can be declared like this enum MyEnum { ONE(\"descr for one\"), TWO(\"descr for two\");

in java an enum can be declared like this

enum MyEnum {
  ONE("descr for one"),
  TWO("descr for two");

  private String descr;

  MyEnum(String descr) {
    this.descr=descr;
  }

  public S开发者_StackOverflowtring getDescr() {return this.descr;}
}

therefore we can always call myEnumInstance.getDescr() for getting enum description. It is possible of course to add several variable in constructor and create its corresponding accessor. Is there anything similiar in objective-c ?

thanks


No. Unfortunately for you, there is nothing similar in ObjectiveC.

You can have a Helper Class mapping enums to NSString* though...

Something like this:

typedef enum {
   kONE,
   kTWO
} MyEnum;

And then a class method/message somewhere:

+ (NSString*) getDescriptionFor:(MyEnum)e
{
    switch(e) {
        case kONE:
             return @"descr for one";
        case kTWO:
             return @"descr for two";
        default:
             break;
    }
    return @"";
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消