【发布时间】:2018-10-20 05:23:29
【问题描述】:
我有以下代码在 Swift 2 中使用编译,但在 Swift 4.2 中不会。返回布尔值的范围函数不再是 Calendar 数据类型的一部分,而是 NSCalendar 数据类型的一部分。有没有办法可以使用或格式化此函数以使其在 Swift 4.2 中编译?
extension Calendar {
/**
Returns a tuple containing the start and end dates for the week that the
specified date falls in.
*/
func weekDatesForDate(date: NSDate) -> (start: NSDate, end: NSDate) {
var interval: TimeInterval = 0
var start: NSDate?
range(of: .weekOfYear, start: &start, interval: &interval, for: date as Date)
let end = start!.addingTimeInterval(interval)
return (start!, end)
}
}
我尝试了以下方法,但是范围函数不一样并且无法编译:
extension NSCalendar {
/**
Returns a tuple containing the start and end dates for the week that the
specified date falls in.
*/
func weekDatesForDate(date: NSDate) -> (start: NSDate, end: NSDate) {
var interval: TimeInterval = 0
var start: NSDate?
range(of: .weekOfYear, start: &start, interval: &interval, for: date as Date)
let end = start!.addingTimeInterval(interval)
return (start!, end)
}
}
【问题讨论】:
标签: swift nscalendar