Swift学习笔记 - 位移枚举的按位或运算

在OC里面我们经常遇到一些枚举值可以多选的,需要用或运算来把这些枚举值链接起来,这样的我们称为位移枚举,但是在swift里面却不能这么做,下面来讲解一下如何在swift里面使用

OC的位移枚举的区分

//位移枚举
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
};

//普通枚举
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
    UIViewAnimationTransitionNone,
    UIViewAnimationTransitionFlipFromLeft,
    UIViewAnimationTransitionFlipFromRight,
    UIViewAnimationTransitionCurlUp,
    UIViewAnimationTransitionCurlDown,
};

OC位移枚举的使用

OC里面位移枚举的使用一般用按位或运算符,也就是|运算符。

//OC里位移枚举的定义
    enum UIViewAnimationOptions option = UIViewAnimationOptionRepeat | UIViewAnimationOptionLayoutSubviews;

//OC里普通枚举的定义
    enum UIViewAnimationTransition option = UIViewAnimationTransitionFlipFromLeft;

swift的位移枚举的区分

//位移枚举
public struct UIViewAutoresizing : OptionSetType {
    public init(rawValue: UInt)

    public static var None: UIViewAutoresizing { get }
    public static var FlexibleLeftMargin: UIViewAutoresizing { get }
    public static var FlexibleWidth: UIViewAutoresizing { get }
    public static var FlexibleRightMargin: UIViewAutoresizing { get }
    public static var FlexibleTopMargin: UIViewAutoresizing { get }
    public static var FlexibleHeight: UIViewAutoresizing { get }
    public static var FlexibleBottomMargin: UIViewAutoresizing { get }
}

//普通枚举
public enum UIViewAnimationTransition : Int {

    case None
    case FlipFromLeft
    case FlipFromRight
    case CurlUp
    case CurlDown
}

swift位移枚举的使用

swift里面位移枚举的用法跟OC就完全不一样了,当你去用按位或的运算符时系统会报错,在swift里面应该用数组来表示:

//swift里面位移枚举的定义
    let option:UIViewAnimationOptions = [.repeat, .layoutSubviews]

//swift里面普通枚举的定义
    let option:UIViewAnimationTransition = .flipFromLeft

以上就是关于swift里面位移枚举的使用小结,如果写的有什么不对的欢迎大家补充,希望大家能学到,谢谢大家的阅读~

上一篇:Nginx+Keepalived搭建高可用负载均衡集群


下一篇:JavaScript学习笔记-实现枚举类型,扑克牌应用