【问题标题】:Can't assign to Int from Date extension's function无法从日期扩展的功能分配给 Int
【发布时间】:2019-04-10 19:16:08
【问题描述】:

我需要知道两个日期之间有多少天,所以我使用了我在网上找到的这段代码

extension Date {

    func years(_ sinceDate: Date) -> Int? {
        return Calendar.current.dateComponents([.year], from: sinceDate, to: self).year
    }

    func months(_ sinceDate: Date) -> Int? {
        return Calendar.current.dateComponents([.month], from: sinceDate, to: self).month
    }

    func days(_ sinceDate: Date) -> Int? {
        return Calendar.current.dateComponents([.day], from: sinceDate, to: self).day
    }

    func hours(_ sinceDate: Date) -> Int? {
        return Calendar.current.dateComponents([.hour], from: sinceDate, to: self).hour
    }

    func minutes(_ sinceDate: Date) -> Int? {
        return Calendar.current.dateComponents([.minute], from: sinceDate, to: self).minute
    }

    func seconds(_ sinceDate: Date) -> Int? {
        return Calendar.current.dateComponents([.second], from: sinceDate, to: self).second
    }  
}

这是 Date 类的扩展。

我在使用这些功能时遇到问题,我有这个功能

func countDays() {
        if let daysLeft = Date.days(targetDate) {
            self.daysLeft = daysLeft
        }
    }

但 Xcode 告诉我

条件绑定的初始化器必须是 Optional 类型,而不是 '(Date) -> Int?'

所以,然后我尝试了这个功能

func countDays() {
        let daysLeft = Date.days(targetDate)
        self.daysLeft = daysLeft
    }

Xcode 仍然告诉我

无法分配类型“(日期)-> Int?”的值输入'Int'

我已尝试构建该项目几次,因为我认为这可能是一个错误,但问题仍然存在。

谁能指出这可能是什么问题?提前致谢!

编辑:我已经尝试像这样强制解开返回的值

func years(_ sinceDate: Date) -> Int {
        return Calendar.current.dateComponents([.year], from: sinceDate, to: self).year!
    }

Xcode 仍然告诉我同样的事情 无法将类型 '(Date) -> Int' 的值赋给类型 'Int'

【问题讨论】:

  • 与您的问题无关,但您为什么不简单地强制打开结果?每个日期都有年份部分。它永远不会失败。
  • func years(_ sinceDate: Date) -> Int { return Calendar.current.dateComponents([.year], from: sinceDate, to: self).year! }
  • let years = Date().years(since: targetDate)
  • 试试extension Date { static func days(since date: Date) -> Int { return Calendar.current.dateComponents([.day], from: date, to: Date()).day! } } Date.days(since: targetDate)
  • 请注意,在这种情况下,我不建议将您的方法声明为静态。我会将其保留为实例方法。我实际上会使用计算属性extension Date { var daysSinceNow: Int { return Calendar.current.dateComponents([.day], from: Date(), to: self).day! } } Calendar.current.date(byAdding: DateComponents(day: -2), to: Date())!.daysSinceNow // -2

标签: ios swift


【解决方案1】:

您正在调用Date 类型的函数。但是,函数不是static。您必须创建一个日期实例并在其上调用函数:

let now = Date()
if let daysLeft = now.days(targetDate) {
   self.daysLeft = daysLeft
}

【讨论】:

  • 之前我尝试将扩展的函数声明为静态,但它给了我这个错误:在扩展文件中没有更多上下文的表达式类型不明确。有没有更简洁的方法来编写这个函数,这样我只需要编写 Date.days(...)?
  • @MattiaRighetti 您正在计算两个日期之间的差异。要么您必须将两者都作为参数传递,要么 self 就是其中之一。仅当您将两者都作为参数传递时,您才能使用 static 函数。
  • 没错,调用 Date() 可以解决问题,但是我需要如何声明该函数才能在 Date 之后没有 () 的情况下调用 Date.days(tgetDate)?在 func 前面写 static 会让 Xcode 抱怨
  • @MattiaRighetti 我再说一遍,如果你想调用 Date.days(...) 那么你有两个参数,例如static func days(since: Date, target: Date).
【解决方案2】:

为什么不使用日历类的标准功能

let days = Calendar.current.dateComponents([.day], from: date1, to: date2)

例子

let date1 = Date(timeIntervalSinceNow: -800000)
print(date1)
let date2 = Date()
print(date2)

let days = Calendar.current.dateComponents([.day], from: date1, to: date2)
if let count = days.day {
    print(count)
}

产量

2019-04-01 13:24:14 +0000
2019-04-10 19:37:34 +0000
9

【讨论】:

  • 我只是想让代码更简洁,冗余代码更少,这就是我写这个扩展的原因。
猜你喜欢
  • 1970-01-01
  • 2020-08-10
  • 2012-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多