Linux中的Swift Timer

Linux中的Swift Timer,第1张

概述你能帮忙,如何在 Linux Ubuntu 16.04上的Swift 4中使用Timer实例? 当我尝试做的时候: let timer = Timer.scheduledTimer(timeInterval: 10.0, target: self, selector: #selector(MyClass.myMethod), userInfo: nil, repeats: true) 我收到错误: 你能帮忙,如何在 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 Foundationimport CoreFoundationlet 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.

总结

以上是内存溢出为你收集整理的Linux中的Swift Timer全部内容,希望文章能够帮你解决Linux中的Swift Timer所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/1021282.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存