如何在日历中使用 NSCalendar 范围函数?
How can I use NSCalendar range function within Calendar?
我有以下代码,在 Swift 2 中使用了编译,但在 Swift 4.2 中不会。 returns 布尔值的范围函数不再是日历数据类型的一部分,而是 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)
}
}
Calendar
中的range(of:start:interval:for:)
相当于dateInterval(of:start:interval:for:)
不要在 Swift
中使用 NSDate
extension Calendar {
/**
Returns a tuple containing the start and end dates for the week that the
specified date falls in.
*/
func weekDatesForDate(date: Date) -> (start: Date, end: Date) {
var interval: TimeInterval = 0
var start = Date()
dateInterval(of: .weekOfYear, start: &start, interval: &interval, for: date)
let end = start.addingTimeInterval(interval)
return (start, end)
}
}
我建议使用专用 DateInterval
作为 return 值而不是元组:
extension Calendar {
/**
Returns a tuple containing the start and end dates for the week that the
specified date falls in.
*/
func weekDatesForDate(date: Date) -> DateInterval {
var interval: TimeInterval = 0
var start = Date()
dateInterval(of: .weekOfYear, start: &start, interval: &interval, for: date)
let end = start.addingTimeInterval(interval)
return DateInterval(start: start, end: end)
}
}
我有以下代码,在 Swift 2 中使用了编译,但在 Swift 4.2 中不会。 returns 布尔值的范围函数不再是日历数据类型的一部分,而是 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)
}
}
Calendar
中的range(of:start:interval:for:)
相当于dateInterval(of:start:interval:for:)
不要在 Swift
中使用NSDate
extension Calendar {
/**
Returns a tuple containing the start and end dates for the week that the
specified date falls in.
*/
func weekDatesForDate(date: Date) -> (start: Date, end: Date) {
var interval: TimeInterval = 0
var start = Date()
dateInterval(of: .weekOfYear, start: &start, interval: &interval, for: date)
let end = start.addingTimeInterval(interval)
return (start, end)
}
}
我建议使用专用 DateInterval
作为 return 值而不是元组:
extension Calendar {
/**
Returns a tuple containing the start and end dates for the week that the
specified date falls in.
*/
func weekDatesForDate(date: Date) -> DateInterval {
var interval: TimeInterval = 0
var start = Date()
dateInterval(of: .weekOfYear, start: &start, interval: &interval, for: date)
let end = start.addingTimeInterval(interval)
return DateInterval(start: start, end: end)
}
}