问题
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