无法在Swift中让我的计时器在Playground中触发 [英] Can't get my Timer in Swift to fire in Playground

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

问题描述

我正在尝试使用Swift和GCD做一个不错的计时器.我在Matt Neuburg的书中发现了很多博客,甚至条目.我使用后者将自己的变体放在操场上:

I'm trying to make a nice timer using Swift and GCD. I find lots of blogs and even an entry in Matt Neuburg's book. I used the latter to put together my own variant in a playground:

import Foundation
struct Timer {
    private var queue = dispatch_queue_create("timer", nil)
    private var source: dispatch_source_t
    var tick:()->() = {} {
        didSet {
            self.update()
        }
    }
    var rate:Double = 1.0 {
        didSet {
            self.update()
        }
    }

    init() {
        self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.queue)
        self.update()
    }

    func cancel() {
        dispatch_source_cancel(self.source)
    }

    func update() {
        dispatch_source_set_timer(self.source, DISPATCH_TIME_NOW, UInt64(Double(NSEC_PER_SEC) / self.rate), 0)
        dispatch_source_set_event_handler(self.source, self.tick)
    }

    func resume() {
        dispatch_resume(self.source)
    }
}

var timer = Timer()
timer.tick = {
    let now = NSDate()
    print("\(now)")
}
timer.cancel()
timer.resume()

我没有编译错误.但是操场底部的控制台什么也没显示.如果我添加

I have no compilation errors. But the console at the bottom of the playground shows nothing. If I add

timer.tick()

它输出当前日期的结果,显然执行一次滴答功能.我在他的书中非常接近马特(Matt)的榜样(出于教育目的进行了一些小的更改).所以我不知道是否

It outputs the result of the current date, apparently executing tick function once. I followed Matt's example in his book pretty closely (making small changes for education purposes). So I'm left with not knowing if

A)很好,当我将其移至真实代码时也可以正常工作,只是在Playground输出中看不到任何明显的地方 B)某些东西不丢失,而且不是真的发射

A) It's just fine and will work fine when I move it to real code, it just doesn't show anywhere apparent in the Playground output B) Somethings not missing and it's not really firing

推荐答案

一旦源被dispatch_source_cancel取消,它将不再调用其事件处理程序.取消dispatch_resume后,您将无法重新启用该源.您必须创建一个新源.

Once a source has been cancelled by dispatch_source_cancel, it will never call its event handler again. You cannot reenable the source with dispatch_resume after cancelling it; you must create a new source.

要使游乐场正常工作,必须删除对timer.cancel()的呼叫.

To make your playground work, you must remove the call to timer.cancel().

此外,在执行了整个Playground脚本之后,默认情况下会退出Playground进程.如果希望它继续运行,则需要设置XCPlaygroundPage.currentPage.needsIndefiniteExecution = true.

Furthermore, after the entire playground script has been executed, the playground process exits by default. If you want it to keep running, you need to set XCPlaygroundPage.currentPage.needsIndefiniteExecution = true.

因此,使脚本结尾看起来像这样:

So make the end of your script look like this:

var timer = Timer()
timer.tick = {
    let now = NSDate()
    print("\(now)")
}
timer.resume()
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

这篇关于无法在Swift中让我的计时器在Playground中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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