Objective-c enumeration type and string mutual transfer scheme

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.