Translated from http://www.rijiyuelei.me/2012/09/enum-and-string-conversion-in-objective-c/
The use advantages of enumeration types There's nothing to say. However, it is often necessary to convert it with strings to facilitate display or other requirements.
In common scenarios, many are declared first, and then a mapping table is created in the implementation file. This brings the problem is to be divided into two files to manage, bring the management of inconvenient bad. It is also ensured that the mapping table has been initialized before use. Obviously, it's still a bit uncomfortable to use.
It is good to manage a lot if you use macros instead, and the lazy initialization principle can be used for the mapping table, which is created only when access is available.
Enum dpodrecordtype{ dpodrecordtypea = 0, dpodrecordtypecname, dpodrecordtypemx, Dpodrecordtypetxt, Dpodrecordtypens,
DPODRECORDTYPEAAAA, dpodrecordtypesrv, dpodrecordtypeurl }; typedef enum DPODRECORDTYPE Dpodrecordtype; Const Nsarray *___dpodrecordtype; Creates an initialization function. is equivalent to creating a getter function with a macro #define Cdpodrecordtypeget (___dpodrecordtype = = nil? ___dpodrecordtype = [[Nsarray alloc] Initwithobjects:\ @ "A", \ @ "CNAME", \ @ "MX", \ @ "TXT", \ @ "NS", \ @ "AAAA", \ @ " SRV ", \ @" URL ", Nil]: ___dpodrecordtype) //Enumerate to String #define Cdpodrecordtypestring (type) ([ Cdpodrecordtypeget Objectatindex:type]) //String to Enumeration
Disadvantage, may lead to the final compilation of the file will be a little bit bigger.
Objective-c enumeration type and string mutual transfer scheme