倒数计时器发布日期

时间:2019-02-07 19:29:16

标签: swift timer release

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

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

这是Xcode中的错误消息

  

“#selector”的参数不能引用全局函数“ 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)
}

1 个答案:

答案 0 :(得分:0)

您不能在#selector中使用全局功能。因此,将其移入课堂。另外,如果您要使用基于选择器的计时器,则updateTime必须标记为@objc

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

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问题,而且使您摆脱了手动构建日期差成分的格式化字符串的麻烦。