gpt4 book ai didi

swift - 组件的 NSTimeInterval

转载 作者:行者123 更新时间:2023-11-28 09:28:53 25 4
gpt4 key购买 nike

我正在尝试转换 this answer 中的 Objective-C 代码, 在 this answer 中找到更正, swift :

var theTimeInterval = NSTimeInterval()
var calendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)

var date1 = NSDate()
var date2 = NSDate(timeInterval: theTimeInterval, sinceDate: date1)

var unitFlags = NSCalendarUnit(UInt.max)
var info = calendar.components(unitFlags, fromDate:date1, toDate:date2, options:0)

但是,在最后一行,Xcode 给我一个莫名其妙的错误:

调用中的额外参数“toDate”

我通过命令单击查看了 NSCalendar 的代码,我使用的函数签名似乎与它的 components 方法完全匹配。那我做错了什么?

最佳答案

我相信这是 Swift 4 的正确实现。注意:

  • 在组件中使用你需要的任何东西
  • 看起来参数“选项”已被删除(参见 the docs)
    var theTimeInterval = TimeInterval()
var calendar = Calendar(identifier: .gregorian)

var date1 = Date()
var date2 = Date(timeInterval: theTimeInterval, since: date1)

//NSCalendarUnit from objc are Calendar.Component in swift
var components: Set<Calendar.Component> = [.hour, .minute, .second]
var info = calendar.dateComponents(components,
from: date1, to: date2)

关于swift - 组件的 NSTimeInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25245445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com