Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何在日历中使用NSCalendar范围函数?_Swift_Nscalendar - Fatal编程技术网

Swift 如何在日历中使用NSCalendar范围函数?

Swift 如何在日历中使用NSCalendar范围函数?,swift,nscalendar,Swift,Nscalendar,我有以下在Swift 2中使用的代码,但是在Swift 4.2中不会。返回布尔值的范围函数不再是日历数据类型的一部分,而是NSCalendar数据类型的一部分。是否有一种方法可以使用或格式化此函数,使其在Swift 4.2中编译 extension Calendar { /** Returns a tuple containing the start and end dates for the week that the specified date falls in

我有以下在Swift 2中使用的代码,但是在Swift 4.2中不会。返回布尔值的范围函数不再是日历数据类型的一部分,而是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
作为返回值,而不是元组:

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)
    }
}

它表示dateInterval函数未使用。您是说通过将结果强制转换为DateInterval数据类型基本上解决了问题,而不需要DateInterval函数吗?这两个版本的工作方式应该是相同的。它是说dateInterval函数未使用,还是说dateInterval函数的结果未使用。函数本身显然不是未使用的。如果收到关于结果为未使用的警告,请写入
\uu=dateInterval(…