iOS位移枚举

作者: 莫忘尘 | 来源:发表于2017-05-05 18:31 被阅读772次

位移枚举

目录

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

问题引入

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

问题解析

@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];
}

相关文章

  • iOS枚举(位移枚举)

    什么是枚举 在程序设计语言中,一般用一个数值来代表某一状态,这种处理方法不直观,易读性差。如果能在程序中用自然语言...

  • iOS位移枚举

    位移枚举 目录 问题引入 问题解析 概念讲解 问题解决办法(位移枚举) 优化方法 问题引入 假定有一项考试,考试内...

  • iOS 位移枚举

    待完成 每一天坚持学习

  • iOS枚举

    在iOS中定义枚举可以帮我们减轻不少工作枚举定义有两种一种是数值 一种是按照位移 位移的用处在于可以组合使用,比如...

  • 位移枚举

    位移枚举和普通枚举的区别位移枚举可以传递多个参数,普通的枚举只能传递单个参数 举个?在SDWebimage里有的地...

  • Swift&OC位移枚举区别

    1、Swift 位移枚举写法 2、ObjC位移枚举写法 相比之下ObjC更加简单明了。

  • iOS枚举——位移枚举的简单使用

    今天和大家一起来学习一下iOS位移枚举的简单使用,有疏忽的地方,还望各位不吝赐教。 一、枚举的作用 在代码中使用枚...

  • iOS位移枚举的理解

    整数型枚举就是一个萝卜一个坑,一种情况对应一个枚举,但是是单选, 而位移枚举则是可以多选,在使用位移枚举的情况下 ...

  • 位移枚举

    位移枚举 C语言枚举定义 苹果创建枚举的定义方式

  • 位移枚举

    枚举的其中一种方式:位移枚举,直接上代码,看完基本就懂了 写一个方法: 在viewDidLoad方法中调用本方法:

网友评论

    本文标题:iOS位移枚举

    本文链接:https://www.haomeiwen.com/subject/prystxtx.html