iOS开发文集

枚举映射:枚举和字符串的映射,枚举和数字串的映射

2017-06-15  本文已影响12338人  十一岁的加重

有时候也是坑了,会定义很多枚举,然后你还得根据这些不同的枚举来设置不同的显示,比如控制器title,之前一直写if else if 来判断,后面某一天看着这垃圾代码想吐。于是参考了网上的文章,改进了下。

实践过程:
一、定义枚举

/// 进入画页的类型
typedef NS_ENUM(NSInteger, PageType) {
    PageTypeAuthPhoto = 0,//!< 授权书
    PageTypeShopPhoto,//!< 店铺照片
    PageTypeAutomobileSalesQualificationRelatedDocuments,//!< 汽车销售资质相关文件
    PageTypeDrivingLicense,//!< 行驶证
    PageTypePayStrongInsurancePolicy,//!< 交强险保单
    PageTypeCarInvoice,//!< 购车发票
};

二、创建一个C语言的字符串数组

static NSString  * const  PageTypeStringMapping[] = {
    [PageTypeAuthPhoto] = @"选择授权书",
    [PageTypeShopPhoto] = @"选择店铺照片",
    [PageTypeAutomobileSalesQualificationRelatedDocuments] = @"汽车销售资质相关文件",
    [PageTypeDrivingLicense] = @"行驶证",
    [PageTypePayStrongInsurancePolicy] = @"交强险保单",
    [PageTypeCarInvoice] = @"购车发票",
};

三、我们开始使用了

    self.title = PageTypeStringMapping[self.pageType];    

有时候我们也会根据不同的状态产生某种最大值。
比如上传图片页面,在不同的上传图片状态下,会有不同的最大值

static NSInteger const  PageTypeMaxChooseCountMapping[] = {
    [PageTypeAuthPhoto] = 10,
    [PageTypeShopPhoto] = 7,
    [PageTypeAutomobileSalesQualificationRelatedDocuments] = 10,
    [PageTypeDrivingLicense] = 5,
    [PageTypePayStrongInsurancePolicy] = 5,
    [PageTypeCarInvoice] = 5,
};

然后如果用if else if 来处理这个数值就麻烦了,不过现在好的,这样多简单快捷

    NSInteger maxPicNum = PageTypeMaxChooseCountMapping[self.pageType];

参考链接:
http://honglu.me/2015/04/24/iOS%E5%BC%80%E5%8F%91%E7%9A%84%E5%A5%87%E6%8A%80%E6%B7%AB%E5%B7%A7/

上一篇 下一篇

猜你喜欢

热点阅读