Swift OptionSetType按位或

时间:2016-07-08 19:36:56

标签: swift bitwise-or optionsettype

说我有以下OptionSetType

struct Modifier : OptionSetType {

    typealias RawValue = Int
    var rawValue: RawValue = 0

    static let Default: Modifier = [.Public, .Package, .Internal]

    static let Public    = Modifier(rawValue: 1 << 0)
    static let Package   = Modifier(rawValue: 1 << 1)
    static let Protected = Modifier(rawValue: 1 << 2)
    static let Internal  = Modifier(rawValue: 1 << 3)
    static let Private   = Modifier(rawValue: 1 << 4)

    static let Static    = Modifier(rawValue: 1 << 5) 
    static let Final     = Modifier(rawValue: 1 << 6) 

    init(rawValue: RawValue) {
        self.rawValue = rawValue
    }

}

如何确定Modifier类型的值是否包含Modifier.Default中包含的至少一个元素?

2 个答案:

答案 0 :(得分:2)

这个怎么样?

let modifier: Modifier = ...

if Modifier.Default.contains(modifier) {

}

答案 1 :(得分:0)

let m = Modifier()
m.contains([.Public]) // false

let m1 = Modifier.Default
m1.contains([.Public, .Package, .Internal]) // true
m1.contains([.Public])                      // true
// ....