Objective C: convert string to enum

匿名 (未验证) 提交于 2019-12-03 01:38:01

问题:

If I have an enum:

typedef enum {   SOMETHING, } MyEnum

and I have a NSString "SOMETHING", is there a way I can go directly from the string to the ENUM value? I realize I can just make a dictionary to do this, but I'm curious.

回答1:

There isn't really a clean way to do this in Objective-C (or C, for that matter).

You're going to have to map the enum values to their string counterparts. There are a number of ways you can do this: (1) A dictionary, as you mentioned. (2) A switch statement. (3) An array of string values where each index maps to a the corresponding enum value.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!