简体   繁体   English

Swift-如何调用函数的特定日期和时间

[英]Swift - How to call function specific date and time

即使应用程序未运行,我如何每天下午13:00之后运行功能

You should use background task if you want to run the function if your application is on the background, not closed. 如果您的应用程序在后台而不是在后台运行,并且要运行该功能,则应该使用后台任务。 Such as: 如:

var timer = NSTimer(timeInterval: Your nstimerinterval, target: self, selector: "someBackgroundTask:", userInfo: nil, repeats: true)

func someBackgroundTask(timer:NSTimer) { func someBackgroundTask(timer:NSTimer){

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in
        println("You can do background tasks here.")

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            println("If you want to update your UI, you should do it here.")

        })
    })
}

The best way to do this is with an NSTimer . 最好的方法是使用NSTimer Use something like: 使用类似:

var timer = NSTimer.scheduledTimerWithTimeInterval(interval, target: self, selector: "function_name", userInfo: nil, repeats: true)

to create a loop that runs a certain function whenever you want. 创建一个可以随时运行某个功能的循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM