Error combining NSCalendarUnit with OR (pipe) in Swift 2.0

末鹿安然 提交于 2019-12-11 03:51:21

问题


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:


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]


来源:https://stackoverflow.com/questions/32381664/error-combining-nscalendarunit-with-or-pipe-in-swift-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!