swiftbitwise-oroptionsettype

Swift OptionSetType Bitwise-Or


Say I have the following 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
    }

}

How would I determine whether or not a value of type Modifier contains at least one of the elements contained in Modifier.Default?


Solution

  • What about this?

    let modifier: Modifier = ...
    
    if Modifier.Default.contains(modifier) {
    
    }