美文网首页
OptionSet-Swift & OC(UIControlSt

OptionSet-Swift & OC(UIControlSt

作者: yehkong | 来源:发表于2017-05-24 18:55 被阅读0次

    查看swift和OC的UIControlState写法,是不同的:

    Swift写法:

    public struct UIControlState : OptionSet {

    public init(rawValue: UInt)

    public static var normal: UIControlState { get }

    public static var highlighted: UIControlState { get }

    ...

    }

    OC写法:

    typedef NS_OPTIONS(NSUInteger, UIControlState) {

    UIControlStateNormal      = 0,

    UIControlStateHighlighted = 1 << 0,

    ...

    };

    OC写法我们调用就是 (UIControlStateNormal | UIControlStateHighlighted),Swift调用是 ([.normal,.highlighted]);OC好理解,位或运算;但是Swift这是什么鬼呢?

    查找资料后,原来还是很多东西在这里的的。

    OptionSet的声明: public protocol OptionSet : SetAlgebra, RawRepresentable;这是一份协议,继承自SetAlgebra, RawRepresentable协议;实现了OptionSet协议(只有一个方法:public init(rawValue: Self.RawValue))就可以对成员变量进行比较、类似[.normal,.highlighted]这样的列举,交并集等。虽然只实现了一个初始化方法,但是OptionSet的Extension已经帮我们实现了。

    这样一理顺就好理解了。

    参考:(http://www.jianshu.com/p/9a1fcfb7e3ee)

    相关文章

      网友评论

          本文标题:OptionSet-Swift & OC(UIControlSt

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