EnumStringConvert

Generator both Enum and NSString in Objective-C.

  1. Copy the header file to your project

  2. Import the header file

#import "enum_generator.h"
  1. Define your custom Enum
#define WorkStatus(XX) \
XX(WorkStatusUnKnown,) \
XX(WorkStatusWorking,) \
XX(WorkStatusSleeping,=50)

DECLARE_ENUM(WorkStatus, WorkStatus)
  1. enjoy it😆
WorkStatus testWorkStatus = WorkStatusUnKnown;

NSLog(@"workstatus is: %@", stringFromWorkStatus(testWorkStatus));

if (testWorkStatus == WorkStatusFromString(@"WorkStatusUnKnown")) {
    NSLog(@"确认在摸鱼");
}

如果你在天朝

利用宏定义实现枚举转字符串