首页 > 代码库 > OC开发中运用到的枚举

OC开发中运用到的枚举

?

一 ?常见枚举的定义:

typedef enum
{
LOGIN_SUCCESS,
USER_NAME,
USER_PASSWORD,
OLD_LAT,
OLD_LNG

}FIELD_SAVED;

?

二?在iOS6和Mac OS 10.8以后Apple引入了两个宏来重新定义这两个枚举类型,实际上是将enum定义和typedef合二为一,并且采用不同的宏来从代码角度来区分。?

    typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {  
        UIViewAnimationTransitionNone,//默认从0开始  
        UIViewAnimationTransitionFlipFromLeft,  
        UIViewAnimationTransitionFlipFromRight,  
        UIViewAnimationTransitionCurlUp,  
        UIViewAnimationTransitionCurlDown,  
    };  
      
    typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {  
        UIViewAutoresizingNone                 = 0,  
        UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,  
        UIViewAutoresizingFlexibleWidth        = 1 << 1,  
        UIViewAutoresizingFlexibleRightMargin  = 1 << 2,  
        UIViewAutoresizingFlexibleTopMargin    = 1 << 3,  
        UIViewAutoresizingFlexibleHeight       = 1 << 4,  
        UIViewAutoresizingFlexibleBottomMargin = 1 << 5  
    }; 

?

三 枚举经常被用于字符的替换.比如把枚举转化为字符.

? ?枚举转为字符.

-(NSString*) getStringFromField:(FIELD_SAVED)defaultFiled{
    switch (defaultFiled) {
        case LOGIN_SUCCESS:
            return @"login_success";
        case USER_NAME:
            return @"user_name";
        case USER_PASSWORD:
            return @"user_pwd";
        case OLD_LAT:
            return @"lat";
        case OLD_LNG:
            return @"lng";
        default:
            return @"default";
    }
}

?根据请求后的数据,找相应字符串:

1 首先定义一个全部枚举值的数组.

2 根据请求后的数据,取下标.

3 根据下标,取相应的字符串.

?

?

?

OC开发中运用到的枚举