gpt4 book ai didi

swift - 发布日期倒计时器

转载 作者:行者123 更新时间:2023-11-30 10:50:49 24 4
gpt4 key购买 nike

您好,我正在尝试处理此代码,但我的问题出在代码的一部分上:

countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: (Any).self, selector: #selector(updateTime), userInfo: nil, repeats: true)

这是 Xcode 中的错误消息

Argument of '#selector' cannot refer to global function 'updateTime()'

我无法处理这个问题。可能出什么问题了?谢谢你

import UIKit
import Foundation
var releaseDate: NSDate?
var countdownTimer = Timer()

func startTimer() {

let releaseDateString = "2019-02-23 08:00:00"
let releaseDateFormatter = DateFormatter()
releaseDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
releaseDate = releaseDateFormatter.date(from: releaseDateString)! as NSDate

countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: (Any).self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}

func updateTime() {

let currentDate = Date()
let calendar = Calendar.current

let diffDateComponents = calendar.dateComponents([.day, .hour, .minute, .second], from: currentDate, to: releaseDate! as Date)

let countdown = "Days \(diffDateComponents.day ?? 0), Hours \(diffDateComponents.hour ?? 0), Minutes \(diffDateComponents.minute ?? 0), Seconds \(diffDateComponents.second ?? 0)"

print(countdown)
}

最佳答案

您不能在#selector中使用全局函数。所以把它移到一个类中。另外,如果您要使用基于选择器的计时器,则 updateTime 必须标记为 @objc

或者,如果您确实想使用全局函数,只需使用基于 block 的计时器:

var releaseDate: Date?
var countdownTimer = Timer()

func startTimer() {
let releaseDateString = "2019-02-23 08:00:00"
let releaseDateFormatter = DateFormatter()
releaseDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
releaseDate = releaseDateFormatter.date(from: releaseDateString)

countdownTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
updateTime()
}
}

func updateTime() {
let currentDate = Date()
let calendar = Calendar.current

let diffDateComponents = calendar.dateComponents([.day, .hour, .minute, .second], from: currentDate, to: releaseDate!)

let countdown = "Days \(diffDateComponents.day ?? 0), Hours \(diffDateComponents.hour ?? 0), Minutes \(diffDateComponents.minute ?? 0), Seconds \(diffDateComponents.second ?? 0)"

print(countdown)
}

但最好将其放入 structclass 中以避免污染全局命名空间。

(另外,我不确定你为什么使用 NSDate ...我建议保留 Date 对象。)

<小时/>

就我个人而言,我会做这样的事情:

class TimerManager {
var releaseDate: Date?

private weak var countdownTimer: Timer?

private let formatter: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.allowedUnits = [.day, .hour, .minute, .second]
return formatter
}()

deinit {
countdownTimer?.invalidate()
}

func startTimer() {
releaseDate = DateComponents(calendar: .current, year: 2019, month: 2, day: 23, hour: 8).date

countdownTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
self?.updateTime()
}
}

private func updateTime() {
guard let releaseDate = releaseDate,
let string = formatter.string(from: Date(), to: releaseDate) else { return }
print(string)
}
}

这不仅可以解决Timer问题,还可以让您摆脱手动构建日期差异组件的格式化字符串的麻烦。

关于swift - 发布日期倒计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54580881/

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