希望把字符串变成枚举,调用方不需要去输入或查找相应的字符串定义。
经查找,可以用宏进行处理,对搜集的例子行了整理并加上注释,如下:
//enum
typedef enum : NSUInteger {
AVCapturePresetQualityHigh = 0,
AVCapturePresetQualityNormal,
AVCapturePresetQualityLow,
} AVCaptureSessionPresetQuality;
//declare an array contains all cases
const NSArray *__captureQuality;
//macro for array creation
#ifndef AVCaptureQuality
#define AVCaptureQuality \
(__captureQuality == nil ? __captureQuality = \
@[AVCaptureSessionPreset1920x1080,\
AVCaptureSessionPreset1280x720,\
AVCaptureSessionPreset640x480]\
: __captureQuality)
#endif
//enum and string transform
//enum -> string
#ifndef kGetCapturePresetString
#define kGetCapturePresetString(idx) \
([AVCaptureQuality objectAtIndex:idx])
#endif
//string -> enum
#ifndef kGetCapturePresetEnum
#define kGetCapturePresetEnum(string) \
([AVCaptureQuality indexOfObject:string])
#endif
usage:
NSString *presetQuality = kGetCapturePresetString(AVCapturePresetQualityHigh);