Swift:applicationDidEnterBackground 计时器 [英] Swift: applicationDidEnterBackground timer

查看:33
本文介绍了Swift:applicationDidEnterBackground 计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在applicationDidEnterBackground方法中添加一个NSTimer;我尝试添加

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)

但它不起作用.

推荐答案

根据您的评论,您的声明应如下所示:

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

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

注意两点:

  1. 您可以直接将字符串文字传递给选择器参数(不需要 Selector() 语法),但这当然是可选的.
  2. NSTimer 期望选择器在达到零时触发,以接受一个参数(NSTimer 类型),它将传递给它自己,所以一个冒号 (:) 需要在选择器名称的末尾,以指示它将接受一个参数.

  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.

选择器应具有以下签名:timerFireMethod:(包括一个冒号来表示该方法需要一个参数).这timer 将自身作为参数传递,因此该方法将采用以下模式:- (void)timerFireMethod:(NSTimer *)timer

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

来自 NSTimer 文档.

因此,您还必须稍微修改函数声明:

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

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

这篇关于Swift:applicationDidEnterBackground 计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆