2

I have some code that's breaking in Swift 2.0:

let formatter = NSDateComponentsFormatter()
formatter.allowedUnits = NSCalendarUnit.Year
formatter.allowedUnits |= .Month
formatter.allowedUnits |= .WeekOfMonth 
formatter.allowedUnits |= .Day
formatter.allowedUnits |= .Hour
formatter.allowedUnits |= .Minute

I get the error Binary operator '|=' cannot be applied to 'NSCalenderUnit' operands.

What's the new way of doing this kinda thing?

rob
  • 4,069
  • 3
  • 34
  • 41
  • Essentially the same problem (and solution) as http://stackoverflow.com/questions/30761996/swift-2-0-binary-operator-cannot-be-applied-to-two-uiusernotificationtype. – Martin R Sep 03 '15 at 17:47

1 Answers1

7

NSCalendarUnit is an OptionSetType in Swift 2, instead of a RawOptionSetType. This means that you can't logical-or it anymore. Instead, you can use an array literal representation of it:

formatter.allowedUnits = [.Year, .Month, .WeekOfMonth, .Day, .Hour, .Minute]
zneak
  • 134,922
  • 42
  • 253
  • 328