CalendarUnit

How to correctly declare a Calendar Unit Component in iOS9? Here's what i did, and it fails..

let cal = NSCalendar.currentCalendar()
var comps = cal.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay | .CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond, fromDate: NSDate())

Accepted Reply

OptionSets are handled differently now in Swift 2.0 and use set syntax instead of bitwise operators to join and test them.


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

}


See the Xcode 7 beta release notes or either of the following threads for more info:

https://forums.developer.apple.com/thread/4561

https://forums.developer.apple.com/thread/3623

Replies

OptionSets are handled differently now in Swift 2.0 and use set syntax instead of bitwise operators to join and test them.


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

}


See the Xcode 7 beta release notes or either of the following threads for more info:

https://forums.developer.apple.com/thread/4561

https://forums.developer.apple.com/thread/3623

Sorry. I didn't quit get what you mean. Can you please rewrite my code in the correct way? Thanks.

There are examples in both of the threads I linked to.


The first one of the two is probably better, since some of the NSCalendarUnit enum values have updated names.


let cal = NSCalendar.currentCalendar()
var comps = cal.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: NSDate())

Thanks. It solved my problem!! 🙂🙂