微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Linux中的Swift Timer

你能帮忙,如何在Linux Ubuntu 16.04上的Swift 4中使用Timer实例?

当我尝试做的时候:

let timer = Timer.scheduledTimer(timeInterval: 10.0, target: self, selector: #selector(MyClass.myMethod), userInfo: nil, repeats: true)

我收到错误错误:’#selector’只能与Objective-C运行时一起使用

解决方法:

您可以在Linux上使用基于块的计时器功能.这是一个最小的
在Xcode 9.1中编译和运行的自包含示例
并在https://swift.sandbox.bluemix.net/#/repl

import Foundation
import CoreFoundation

let timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { timer in
    print("In timer function")
    exit(0)
}

CFRunLoopRun()

(我添加了exit(0)只是因为IBM Swift SandBox限制了
程序执行时间为5秒.)

或者,使用dispatchSourceTimer进行演示
here.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐