关于枚举

2016-05-22  本文已影响69人  coder_hong

C语言中的另一种数据类型---枚举类型。枚举类型在iOS中也是很常用的

枚举的概念

枚举类型的定义

#import <UIKit/UIKit.h>

enum Season {
    spring,
    summer,
    autumn,
    winter
};

typedef enum {
    SexMale,
    SexFemale
} Sex;
@interface ViewController : UIViewController

@property (nonatomic, assign) Season Sea;
@end
#import <UIKit/UIKit.h>

enum Season {
    spring,
    summer,
    autumn,
    winter
} sea;

@interface ViewController : UIViewController

@property (nonatomic, assign) enum Season Sea;
@end
#import <UIKit/UIKit.h>

enum {
    spring,
    summer,
    autumn,
    winter
} sea;

@interface ViewController : UIViewController

@property (nonatomic, assign) enum Season Sea;
@end

枚举的注意

遍历枚举

enum Season {spring, summer, autumn, winter} s;

// 遍历枚举元素
for (s = spring; s <= winter; s++) {
    printf("枚举元素:%d \n", s);
}

输出结果:


在UIKIT中常见的枚举写法

方式一

#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, HTTableViewCellStyle) {
    HTTableViewCellStyleDefault,
    HTTableViewCellStyleValue1,
    HTTableViewCellStyleValue2,
    HTTableViewCellStyleSubtitle
};

@interface ViewController : UIViewController

@property (nonatomic, assign) enum HTTableViewCellStyle CellStyle ;
@end

方式二 位移操作枚举定义

#import <UIKit/UIKit.h>

typedef NS_OPTIONS(NSUInteger, HTTableViewCellStateMask) {
    HTTableViewCellStateDefaultMask                     = 0,
    HTTableViewCellStateShowingEditControlMask          = 1 << 0,
    HTTableViewCellStateShowingDeleteConfirmationMask   = 1 << 1
};


@interface ViewController : UIViewController

@property (nonatomic, assign) enum HTTableViewCellStateMask StateMasStyle ;
@end


上一篇下一篇

猜你喜欢

热点阅读