加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Linux中的Swift Timer

发布时间:2020-12-13 23:53:32 所属栏目:Linux 来源:网络整理
导读:你能帮忙,如何在 Linux Ubuntu 16.04上的Swift 4中使用Timer实例? 当我尝试做的时候: let timer = Timer.scheduledTimer(timeInterval: 10.0,target: self,selector: #selector(MyClass.myMethod),userInfo: nil,repeats: true) 我收到错误:错误:’#sele
你能帮忙,如何在 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读