本文介绍了在Swift 2.0中将NSCalendarUnit与OR(管道)结合在一起时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些在Swift 2.0中中断的代码:

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

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

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

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

推荐答案

NSCalendarUnit是Swift 2中的OptionSetType,而不是RawOptionSetType.这意味着您将无法再进行逻辑处理.相反,您可以使用它的数组文字表示形式:

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]

这篇关于在Swift 2.0中将NSCalendarUnit与OR(管道)结合在一起时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-20 22:33