计时器不在Swift 3.0游乐场中运行

时间:2023-01-23 22:29:07

Working in playground with Swift 3.0 I have this code:

使用Swift 3.0在游乐场工作我有这个代码:

struct Test {
    func run() {
        var timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { timer in
            print("pop")
        }
    }
}

let test = Test()
test.run()

But nothing is printing to console. I've read How can I use NSTimer in Swift? and most of the usage of timer that I've seen in answers and tutorials online involves a selector so I tried this:

但没有什么是打印到控制台。我读过如何在Swift中使用NSTimer?我在网上的答案和教程中看到的计时器的大部分用法涉及一个选择器,所以我尝试了这个:

class Test {
    func run() {
        var timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(self.peep), userInfo: nil, repeats: false)
    }

    @objc func peep() {
        print("peep")
    }
}

let test = Test()
test.run()

Still nothing seems to print to console. If I add timer.fire(), then I get the console prints, but obviously that defeats the purpose. What do I need to change to get the timer to run?

似乎仍然没有打印到控制台。如果我添加timer.fire(),那么我得到控制台打印,但显然这违背了目的。我需要更改什么才能让计时器运行?

EDIT:

编辑:

So adding CFRunLoopRun() after I called the run method for my Test struct did the trick. Much Thanks to those who answered, especially @AkshayYaduvanshi (who's comment pointed me to CFRunLoopRun()) and @JoshCaswell (whose answer brought up the fact that I timer only works with a run loop).

因此,在为我的Test结构调用run方法后添加CFRunLoopRun()就可以了。非常感谢那些回答的人,特别是@AkshayYaduvanshi(他的评论指向我CFRunLoopRun())和@JoshCaswell(他的答案提出了我的计时器仅适用于运行循环的事实)。

2 个解决方案

#1


7  

You need to start a run loop.

您需要启动一个运行循环。

RunLoop.main.run(until: Date(timeIntervalSinceNow: 3))

The timer doesn't do anything unless there is a working run loop accepting input. The program simply ends.

除非有工作运行循环接受输入,否则计时器不会执行任何操作。该计划只是结束。

Timer reference:

定时器参考:

Timers work in conjunction with run loops. [...] it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed.

定时器与运行循环一起使用。 [...]仅当已添加计时器的其中一个运行循环模式正在运行并且能够检查计时器的触发时间是否已经过去时,它才会触发。

#2


17  

Your first version works if you allow the Playground to run indefinitely via PlaygroundSupport:

如果允许Playground通过PlaygroundSupport无限期运行,则第一个版本可以正常运行:

import Foundation
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true 

let timer = Timer.scheduledTimer(withTimeInterval: 3, repeats: true) { timer in
  print("Timer Fired at: \(timer.fireDate)")
}

Adding a run loop manually also works, and might be needed sometimes, but in your case this simple instruction is enough to make the Timer work properly.

手动添加运行循环也有效,有时可能需要,但在这种情况下,这个简单的指令足以使Timer正常工作。

#1


7  

You need to start a run loop.

您需要启动一个运行循环。

RunLoop.main.run(until: Date(timeIntervalSinceNow: 3))

The timer doesn't do anything unless there is a working run loop accepting input. The program simply ends.

除非有工作运行循环接受输入,否则计时器不会执行任何操作。该计划只是结束。

Timer reference:

定时器参考:

Timers work in conjunction with run loops. [...] it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed.

定时器与运行循环一起使用。 [...]仅当已添加计时器的其中一个运行循环模式正在运行并且能够检查计时器的触发时间是否已经过去时,它才会触发。

#2


17  

Your first version works if you allow the Playground to run indefinitely via PlaygroundSupport:

如果允许Playground通过PlaygroundSupport无限期运行,则第一个版本可以正常运行:

import Foundation
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true 

let timer = Timer.scheduledTimer(withTimeInterval: 3, repeats: true) { timer in
  print("Timer Fired at: \(timer.fireDate)")
}

Adding a run loop manually also works, and might be needed sometimes, but in your case this simple instruction is enough to make the Timer work properly.

手动添加运行循环也有效,有时可能需要,但在这种情况下,这个简单的指令足以使Timer正常工作。