Objective-C 枚举转字符串

CREnumHelper.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#import <Foundation/Foundation.h>
//定义枚举类型
/**
* 消息类型
*/
typedef NS_ENUM(NSUInteger, XMPPMessageType){
XMPPMessageTypeText = 0, //文本
XMPPMessageTypeImage, //图片
XMPPMessageTypeVoice, //语音
XMPPMessageTypeSystemTip, //系统消息
XMPPMessageTypeFace, //表情
XMPPMessageTypeVideo, //视频
XMPPMessageTypeLocal //定位
};
static NSString *XMPPMessageTypes[] = {
[XMPPMessageTypeText] = @"文本",
[XMPPMessageTypeImage] = @"图片",
[XMPPMessageTypeVoice] = @"语音",
[XMPPMessageTypeSystemTip] = @"系统消息",
[XMPPMessageTypeFace] = @"表情",
[XMPPMessageTypeVideo] = @"视频",
[XMPPMessageTypeLocal] = @"定位"
};
extern NSString * const XMPPMessageTypeEnumToString(XMPPMessageType Key);
@interface CREnumHelper : NSObject
@end

CREnumHelper.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#import "CREnumHelper.h"
//定义字符串函数
NSString * const XMPPMessageTypeEnumToString(XMPPMessageType type){
switch (type) {
case XMPPMessageTypeText:
return @"文本";
case XMPPMessageTypeImage:
return @"图片";
case XMPPMessageTypeVoice:
return @"语音";
case XMPPMessageTypeSystemTip:
return @"系统消息";
case XMPPMessageTypeFace:
return @"视频";
case XMPPMessageTypeVideo:
return @"文本";
case XMPPMessageTypeLocal:
return @"定位";
default:
return @"未知格式";;
}
}
@implementation CREnumHelper
@end

调用的时候:

1
2
NSString *string = XMPPMessageTypeEnumToString(XMPPMessageTypeText);
NSLog(@"%@",string);

另一种方式:

1
2
NSString *string2 = XMPPMessageTypes[XMPPMessageTypeLocal];
NSLog(@"%@",string2);

以上两种做法,只是提供了方便枚举转字符串,但相应反过来时,就无法做到了,而在我之前项目时,项目中交互时,会传一大堆的如”1000”,”1001”,”2002”…各种各样的指令集,如果你在项目中一大堆[@”1000” isEqualToString obj],那写得是有多恶心,而这样的代码又有谁看得懂?所以在这基础上,我做了另外一种写法,可以满足两边来回切转,采用的是字典的做法。

CREnumHelper.h

1
2
3
4
5
6
7
8
@interface CREnumHelper : NSObject
+(XMPPMessageType)coro_getMessageTypeByMessageTypeString:(NSString *)messageTypeStr;
+ (NSString *)coro_getMessageTypeStringByMessageType:(XMPPMessageType)messageType;
@end

CREnumHelper.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
static NSDictionary *dict ;
@implementation CREnumHelper
+ (void)load {
dict = @{
@(XMPPMessageTypeText): @"文本",
@(XMPPMessageTypeImage): @"图片",
@(XMPPMessageTypeVoice): @"语音",
@(XMPPMessageTypeSystemTip): @"系统消息",
@(XMPPMessageTypeFace): @"表情",
@(XMPPMessageTypeVideo): @"视频",
@(XMPPMessageTypeLocal): @"定位"
};
}
+ (XMPPMessageType)coro_getMessageTypeByMessageTypeString:(NSString *)messageTypeStr {
__block XMPPMessageType messageType;
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
if ([obj isEqualToString:obj]) {
messageType = [key integerValue];
*stop = YES;
}
}];
return messageType;
}
+ (NSString *)coro_getMessageTypeStringByMessageType:(XMPPMessageType)messageType {
__block NSString *messageTypeStr = nil;
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
if ([key integerValue] == messageType) {
messageTypeStr = obj;
*stop = YES;
}
}];
return messageTypeStr;
}
@end

调用的时候:

1
2
NSLog(@"文本的枚举值是%lu",(unsigned long)[CREnumHelper coro_getMessageTypeByMessageTypeString:@"文本"]);
NSLog(@"XMPPMessageTypeLocal的相应值是%@",[CREnumHelper coro_getMessageTypeStringByMessageType:XMPPMessageTypeLocal]);

通过以上做法,当你从服务器接收到数据时转换成枚举在项目中使用,而当你需要给服务器发送或者存储时,你又可以转换回去,这样在写代码可读性上,就大大增强了,而且一次定义,你不需要过多的去关注数据本身细节,上面提供了三种方式,各种看官各取所需吧!
需要demo可以请点击此处.


注:版权声明:本文为博主原创文章,未经博主允许不得转载。