簡體   English   中英

如何在Swift3中執行timeIntervalSinceNow?

[英]How to do timeIntervalSinceNow in Swift3?

我正在發布通知並觀看教程,當我輸入時:

notification.fireDate = NSDate(timeIntervalSinceNow: 0)

它說

參數標簽'(timeIntervalSinceNow :)'與任何可用的重載都不匹配

我該如何解決? 這是我的代碼:

import UIKit
import UserNotifications

class ViewController: UIViewController {
    var timer = Timer()
    var time = 10
    override func viewDidLoad() {
        super.viewDidLoad()
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: Selector(("Notification")), userInfo: nil, repeats: true)
        // Do any additional setup after loading the view, typically from a nib.
    }
    func notification() {
        time -= 1
        if time <= 0 {
            let notification = UILocalNotification()
            notification.alertAction = "Call"
            notification.alertBody = "You have a call right now"
            notification.fireDate = NSDate(timeIntervalSinceNow: 0)
            UIApplication.shared.scheduleLocalNotification(notification)
            timer.invalidate()
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func pushNotification(_ sender: AnyObject) {
        let AlertView = UIAlertController(title: "Time for your call!", message: "Press go to continue", preferredStyle:  .alert)
        AlertView.addAction(UIAlertAction(title: "Go", style: .default, handler: nil))
        self.present(AlertView, animated: true, completion: nil)
    }
}

斯威夫特3

let minute:TimeInterval = 11.0 * 60.0;
Date(timeIntervalSinceNow: minute);

到現在為+11分鍾。

如果您想要現在的開火日期,它只是:

notification.fireDate = Date()

正如Leo在別處指出的那樣,選擇器是不正確的。 它應該是:

weak var timer: Timer?
var time = 10

override func viewDidLoad() {
    super.viewDidLoad()

    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(handleTimer(_:)), userInfo: nil, repeats: true)
}

func handleTimer(_ timer: Timer) {
    time -= 1

    if time <= 0 {
        let notification = UILocalNotification()

        notification.alertAction = "Call"
        notification.alertBody = "You have a call right now"
        notification.fireDate = Date()

        UIApplication.shared.scheduleLocalNotification(notification)

        timer.invalidate()
    }
}

然后你問:

這肯定有效,但現在我離開應用程序時沒有得到通知

當您離開應用程序時,您不會收到通知,因為當應用程序未運行時, Timer不會繼續觸發。 最好完全取消計時器,並立即安排本地通知所需的時間。 例如,要在10秒內安排本地通知:

override func viewDidLoad() {
    super.viewDidLoad()

    scheduleLocalNotification(delay: 10)
}

func scheduleLocalNotification(delay: TimeInterval) {
    let notification = UILocalNotification()

    notification.alertAction = "Call"
    notification.alertBody = "You have a call right now"
    notification.fireDate = Date().addingTimeInterval(delay)

    UIApplication.shared.scheduleLocalNotification(notification)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM