Error combining NSCalendarUnit with OR (pipe) in S

2019-07-13 23:44发布

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?

1条回答
ら.Afraid
2楼-- · 2019-07-13 23:54

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]
查看更多
登录 后发表回答