简体   繁体   English

如何在Swift3中执行timeIntervalSinceNow?

[英]How to do timeIntervalSinceNow in Swift3?

I was making a notification and watching a tutorial on it and when I typed in: 我正在发布通知并观看教程,当我输入时:

notification.fireDate = NSDate(timeIntervalSinceNow: 0)

it says 它说

Argument labels '(timeIntervalSinceNow:)' do not match any available overloads 参数标签'(timeIntervalSinceNow :)'与任何可用的重载都不匹配

How do I fix this? 我该如何解决? Here is my code: 这是我的代码:

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)
    }
}

Swift 3 斯威夫特3

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

for +11 minutes to now. 到现在为+11分钟。

If you want the fire date to be now, it's just: 如果您想要现在的开火日期,它只是:

notification.fireDate = Date()

As Leo noted elsewhere, the selector is incorrect. 正如Leo在别处指出的那样,选择器是不正确的。 It should be: 它应该是:

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()
    }
}

You then asked: 然后你问:

that definitely works but now I don't get a notification when I leave the app 这肯定有效,但现在我离开应用程序时没有得到通知

You don't get the notification when you leave your app, because the Timer doesn't continue to fire when the app isn't running. 当您离开应用程序时,您不会收到通知,因为当应用程序未运行时, Timer不会继续触发。 It's better to eliminate the timer altogether and just immediately schedule the local notification for the desired time. 最好完全取消计时器,并立即安排本地通知所需的时间。 For example, to schedule a local notification to fire in 10 seconds: 例如,要在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