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

Swift 2中的新@convention(c):我该如何使用它?

发布时间:2020-12-14 05:49:40 所属栏目:百科 来源:网络整理
导读:迁移到Swift 2之后,我遇到了一个错误,指出我现在应该使用@convention(c)(T) – 我试过排列,但到目前为止还没有运气. func foo(context: AnyObject?,width: CGFloat) - Int {}let bar = unsafeBitCast(foo,CFunctionPointer(UnsafeMutablePointerVoid,Float)
迁移到Swift 2之后,我遇到了一个错误,指出我现在应该使用@convention(c)(T) – >我试过排列,但到目前为止还没有运气.
func foo(context: AnyObject?,width: CGFloat) -> Int {

}

let bar = unsafeBitCast(foo,CFunctionPointer<(UnsafeMutablePointer<Void>,Float) -> Int>.self)
您不再需要在Swift 2中创建CFunctionPointer.相反,您可以通过调用约定来注释您的类型,在本例中为c,并直接使用它.
typealias CFunction = @convention(c) (UnsafeMutablePointer<Void>,Float) -> Int
let bar = unsafeBitCast(foo,CFunction.self)

The Swift Programming Language的Type Attributes部分中@convention描述的相关位是:

The c argument is used to indicate a C function reference. The function value carries no context and uses the C calling convention.

(编辑:李大同)

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

    推荐文章
      热点阅读