在Swift 2.0中将NSCalendarUnit与OR(管道)相结合时出错

时间:2015-09-03 17:08:22

标签: swift nsdateformatter swift2 nscalendar nsdatecomponents

我有一些在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

我收到错误Binary operator '|=' cannot be applied to 'NSCalenderUnit' operands

做这种事的新方法是什么?

1 个答案:

答案 0 :(得分:7)

NSCalendarUnit是Swift 2中的OptionSetType,而不是RawOptionSetType。这意味着你不能逻辑 - 或者它不再合乎逻辑。相反,您可以使用它的数组文字表示:

formatter.allowedUnits = [.Year, .Month, .WeekOfMonth, .Day, .Hour, .Minute]