Swift&OC位移枚举区别

2020-05-20  本文已影响0人  小王在努力

1、Swift 位移枚举写法

public struct ATRefreshOption :OptionSet {
    public let rawValue : Int
    public init(rawValue: Int) {
        self.rawValue = rawValue
    }
    public static var none          : ATRefreshOption{return ATRefreshOption(rawValue: 0)}
    public static var header        : ATRefreshOption{return ATRefreshOption(rawValue: 1<<0)};
    public static var footer        : ATRefreshOption{return ATRefreshOption(rawValue: 1<<1)};
    public static var autoHeader    : ATRefreshOption{return ATRefreshOption(rawValue: 1<<2)};
    public static var autoFooter    : ATRefreshOption{return ATRefreshOption(rawValue: 1<<3)};
    public static var defaultHidden : ATRefreshOption{return ATRefreshOption(rawValue: 1<<4)};
    public static var defaults      : ATRefreshOption{return ATRefreshOption(rawValue: header.rawValue|autoHeader.rawValue|footer.rawValue|defaultHidden.rawValue)};
}

2、ObjC位移枚举写法

typedef NS_ENUM(NSUInteger, ATRefreshOption) {
    ATRefreshNone         = 0,
    ATHeaderRefresh       = 1 << 0,
    ATFooterRefresh       = 1 << 1,
    ATHeaderAutoRefresh   = 1 << 2,
    ATFooterAutoRefresh   = 1 << 3,
    ATFooterDefaultHidden = 1 << 4,
    ATRefreshDefault = (ATHeaderRefresh | ATHeaderAutoRefresh | ATFooterRefresh | ATFooterDefaultHidden),
};

相比之下ObjC更加简单明了。

上一篇 下一篇

猜你喜欢

热点阅读