Swift: applicationDidEnterBackground timer

馋奶兔 提交于 2019-12-25 03:34:50

问题


I need to add an NSTimer in the applicationDidEnterBackground method; I tried adding

NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("test"), userInfo: nil, repeats: true)

But it's not working.


回答1:


According to your comments, your declaration should look something like this:

NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: "test:", userInfo: nil, repeats: false)

Note two things:

  1. You can pass a string literal to the selector parameter directly (no need for Selector() syntax), but this is of course optional.
  2. The NSTimer expects the selector that will fire when it hits zero to accept a parameter (of type NSTimer) to which it will pass itself, so a colon (:) is needed at the end of the selector name to indicate that it will take a parameter.

    The selector should have the following signature: timerFireMethod: (including a colon to indicate that the method takes an argument). The timer passes itself as the argument, thus the method would adopt the following pattern: - (void)timerFireMethod:(NSTimer *)timer

    From the NSTimer documentation.

    Because of this, you'll also have to modify your function declaration a bit:

    func test(timer: NSTimer) {
        // Proceed to grab location from background
    }
    


来源:https://stackoverflow.com/questions/28840510/swift-applicationdidenterbackground-timer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!