Binary operator cannot be applied: NSCalendarUnit

Have the following code running in 7.0 beta (7A120f) - didn't see that as a topic in the "beta" forum...adjusting to the forum change.


let dateComponentsFormatter = NSDateComponentsFormatter()
dateComponentsFormatter.allowedUnits = NSCalendarUnit.Day | NSCalendarUnit.Hour | NSCalendarUnit.Minute | NSCalendarUnit.Second


Receiving the following build error: Binary operator '|' cannot be applied to two NSCalendarUnit operands.


Did not see this error in Xcode 6, not sure if this should be filed as a bug report at this juncture. Other solutions offered by the interwebs proving unviable.


Thoughts?


Cheers,

Josh

Accepted Reply

And, just in case the description in the documentation isn't clear enough:


    let dateComponentsFormatter = NSDateComponentsFormatter()
    dateComponentsFormatter.allowedUnits = [NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute, NSCalendarUnit.Second]

Replies

let dateComponentsFormatter = NSDateComponentsFormatter()
dateComponentsFormatter.allowedUnits = [NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute, NSCalendarUnit.Second]


http://adcdownload.apple.com/WWDC_2015/Xcode_7_beta/Xcode_7_beta_Release_Notes.pdf underSwift Language Changes

For the sake of future searchers, here's the relevant excerpt:


• NS_OPTIONS types get imported as conforming to the OptionSetType protocol, which presents a set-like interface for options. (18069205)

Instead of using bitwise operations such as:

// Swift 1.2:

object.invokeMethodWithOptions(.OptionA | .OptionB)

object.invokeMethodWithOptions(nil)

if options & .OptionC == .OptionC {

// .OptionC is set

}

Option sets support set literal syntax, and set-like methods such as contains: object.invokeMethodWithOptions([.OptionA, .OptionB])

object.invokeMethodWithOptions([])

if options.contains(.OptionC) {

// .OptionC is set

}

And, just in case the description in the documentation isn't clear enough:


    let dateComponentsFormatter = NSDateComponentsFormatter()
    dateComponentsFormatter.allowedUnits = [NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute, NSCalendarUnit.Second]

Or just


let dateComponentsFormatter = NSDateComponentsFormatter() 
dateComponentsFormatter.allowedUnits = [.Day, .Hour, .Minute, .Second]


since the enumeration type can be inferred from the context.