iOS位移枚举

位移枚举

目录

  • 问题引入
  • 问题解析
  • 概念讲解
  • 问题解决办法(位移枚举)
  • 优化方法

问题引入

假定有一项考试,考试内容为辨别左、右两个方向,辨认正确得一分.写出考试和得分方法.

问题解析

@interface Test : NSObject

@property (nonatomic, assign) NSInteger count;

@end

@implementation Test

// 辨认方向
- (void)recognizeLeft:(BOOL)isLeft
{
    if (isLeft) {
        _count += 1;
    }
}

- (void)recognizeRight:(BOOL)isRight
{
    if (isRight) {
        _count += 1;
    }
}

// 得分
- (NSInteger)testGrade
{
    return _count;
}

@end

上述为最直接的方法,先调用辨认方向的方法,后查看成绩.

如果此时左、右代表两大块;左细分为左上,正左,左下;同理,右细分为右上,正右,右下.并且左上,左下, 右上,右下,辨认分值为2分.

那此时,recognizeLeft, recognizeRight各自变换成细分的三个方法,虽然能解决,但需要增加很多辨认方法,不是一个好的解决方式.

此时,把左(isLeft),右(isRight) 从BOOL值,转成枚举值,可解决上面问题.

typedef NS_ENUM (NSInteger,LeftType){
    LeftTypeUnknown,    // 不辨认左方向
    LeftTypeTop,        // 左上方
    LeftTypeMiddle,     // 正左方
    LeftTypeBottom      // 左下方
};

typedef NS_ENUM (NSInteger,RightType){
    RightTypeUnknown,   // 不辨认右方向
    RightTypeTop,       // 右上方
    RightTypeMiddle,    // 正右方
    RightTypeBottom     // 右下方
};

原来的recognizeLeft, recognizeRight修改:

// 辨认方向
- (void)recognizeLeft:(LeftType)leftType
{
    if (leftType == LeftTypeTop) {
        
        _count += 2;
        
    }else if (leftType == LeftTypeMiddle) {
    
        _count += 1;
        
    }else if (leftType == LeftTypeBottom) {
    
        _count += 2;
        
    }
}

- (void)recognizeRight:(RightType)rightType
{
    if (rightType == RightTypeTop) {
        
        _count += 2;
        
    }else if (rightType == RightTypeMiddle) {
    
        _count += 1;
        
    }else if (rightType == RightTypeBottom) {
    
        _count += 2;
    }
}

这样,当辨认左上方时,若能辨认则传LeftTypeTop,不能则传LeftTypeUnknown或不调用该方法.其他各方向同理.这样存在的问题是,虽然方法数没增加,但是调用次数增多了.

下面,引入位移枚举的方法讲解.

概念讲解

位移枚举的定义:

typedef NS_OPTIONS (NSInteger,EnumType){
    EnumTypeValue1      = 1 << 0, // 0b00000001
    EnumTypeValue2      = 1 << 1, // 0b00000010
    EnumTypeValue3      = 1 << 2, // 0b00000100
    EnumTypeValue4      = 1 << 3  // 0b00001000
};

问题解决办法(位移枚举)

typedef NS_OPTIONS (NSInteger,LeftType){
    LeftTypeUnknown     = 1 << 0,   // 不辨认左方向
    LeftTypeTop         = 1 << 1,   // 左上方
    LeftTypeMiddle      = 1 << 2,   // 正左方
    LeftTypeBottom      = 1 << 3    // 左下方
};

typedef NS_OPTIONS (NSInteger,RightType){
    RightTypeUnknown    = 1 << 0,   // 不辨认右方向
    RightTypeTop        = 1 << 1,   // 右上方
    RightTypeMiddle     = 1 << 2,   // 正右方
    RightTypeBottom     = 1 << 3    // 右下方
};
// 辨认方向
- (void)recognizeLeft:(LeftType)leftType
{
    if (leftType & LeftTypeTop) {
        
        _count += 2;
        
    }
    if (leftType & LeftTypeMiddle) {
    
        _count += 1;
        
    }
    if (leftType & LeftTypeBottom) {
    
        _count += 2;
        
    }
}

- (void)recognizeRight:(RightType)rightType
{
    if (rightType & RightTypeTop) {
        
        _count += 2;
        
    }
    if (rightType & RightTypeMiddle) {
    
        _count += 1;
        
    }
    if (rightType & RightTypeBottom) {
    
        _count += 2;
    }
}

这样,我们调用的时候,就可以先赋值好枚举变量,调用辨认方法就可以了.

// 某人,左上,正左,左下,右上,右下都能辨认
- (void)test
{
    LeftType leftType = LeftTypeTop | LeftTypeMiddle | LeftTypeBottom;
    [self recognizeLeft:leftType];
    
    RightType rightType = RightTypeTop | RightTypeBottom;
    [self recognizeRight:rightType];
}

还是需要调用左,右两个方法,有了位移枚举,可以合并成一个方法.

优化方法

左、右枚举,合并

typedef NS_OPTIONS (NSInteger,OrientationType){
    OrientationTypeUnknown      = 1 << 0,    // 不辨认方向
    OrientationTypeLeftTop      = 1 << 1,    // 左上方
    OrientationTypeLeftMiddle   = 1 << 2,    // 正左方
    OrientationTypeLeftBottom   = 1 << 3,    // 左下方
    OrientationTypeRightTop     = 1 << 4,    // 右上方
    OrientationTypeRightMiddle  = 1 << 5,    // 正右方
    OrientationTypeRightBottom  = 1 << 6     // 右下方
};

辨认方法合并

- (void)recognizeOrientation:(OrientationType)orientationType
{
    if (orientationType & OrientationTypeLeftTop) {
        
        _count += 2;
        
    }
    if (orientationType & OrientationTypeLeftMiddle) {
        
        _count += 1;
    
    }
    if (orientationType & OrientationTypeLeftBottom) {
        
        _count += 2;
        
    }
    if (orientationType & OrientationTypeRightTop) {
        
        _count += 2;
        
    }
    if (orientationType & OrientationTypeRightMiddle) {
        
        _count += 1;
        
    }
    if (orientationType & OrientationTypeRightBottom) {
        
        _count += 2;
        
    }
    if (orientationType & OrientationTypeUnknown) {
        
    }
}

调用方法:

// 某人,左上,正左,左下,右上,右下都能辨认
- (void)test
{
    OrientationType orientationType = OrientationTypeLeftTop | OrientationTypeLeftMiddle | OrientationTypeLeftBottom | OrientationTypeRightTop | OrientationTypeRightBottom;
    [self recognizeOrientation:orientationType];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,143评论 25 709
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,817评论 18 399
  • P:护士被蒙在鼓里发奖金已经好多年了,现在创建优质护理示范医院,推行护士绩效考核评分与护士奖金垂直发放,在某些医院...
    世涂花开阅读 596评论 0 0
  • 今天打开简书写东西时,发现整个界面和布局焕然一新,感觉挺爽的,瞬间醒悟:人的思维系统、精神风貌、做人处事必须像互联...
    超級奶爸阅读 278评论 0 0
  • 上周谈到“选择”的方法论,这周就讲了“创业的选择”,每一个“概念”是那么地紧紧相扣,上下呼应,还不时插入之前的内容...
    快乐坚强阅读 786评论 0 0