Swift 2.0日历组件错误
您好我在swift 2.0中遇到此错误
Hi I get this error in swift 2.0
无法使用类型'的参数列表调用'components'(NSCalendarUnit,fromDate:NSDate?,toDate:NSDate? ,options:nil)'
Cannot invoke 'components' with an argument list of type '(NSCalendarUnit, fromDate: NSDate?, toDate: NSDate?, options: nil)'
var hoy = dataFormatter.stringFromDate(NSDate())
var despues = dataFormatter.stringFromDate(fecha)
var calHoy = dataFormatter.dateFromString(hoy)
var calcDesp = dataFormatter.dateFromString(despues)
let cal = NSCalendar.currentCalendar()
let unit:NSCalendarUnit = .Day
let components = cal.components(unit, fromDate: calcDesp, toDate: calHoy, options: nil)
从Swift 2开始, NS_OPTIONS
(例如 NSCalendarOptions
)作为 OptionSetType
映射到Swift,它提供类似集合的接口。特别是,无选项
现在可以指定为 []
而不是 nil
:
As of Swift 2, NS_OPTIONS
(such as NSCalendarOptions
) are mapped to Swift as a OptionSetType
which offers a set-like interface. In particular, "no options"
can now be specified as []
instead of nil
:
let components = cal.components(unit, fromDate: calcDesp!, toDate: calHoy!,
options: [])
另见 Swift 2.0-Binary Operator|"无法应用于两个UIUserNotificationType操作数
以及最近添加的如何在Swift中创建NS_OPTIONS样式的位掩码枚举?以获取更多信息。
See also Swift 2.0 - Binary Operator "|" cannot be applied to two UIUserNotificationType operands and the recently added answers to How to create NS_OPTIONS-style bitmask enumerations in Swift? for more information.