Swift 2中的新@convention(c):如何使用? [英] New @convention(c) in Swift 2: How can I use it?

查看:175
本文介绍了Swift 2中的新@convention(c):如何使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

迁移到Swift 2之后,出现了一个错误,指出我现在应该使用@convention(c)(T)->U.我已经尝试了排列,但到目前为止没有运气.

After migrating to Swift 2, I am getting this issue with an error stating that I should now use @convention(c) (T) -> U. I've tried permutations but so far no luck.

func foo(context: AnyObject?, width: CGFloat) -> Int {

}

let bar = unsafeBitCast(foo, CFunctionPointer<(UnsafeMutablePointer<Void>, Float) -> Int>.self)

推荐答案

您不再需要在Swift 2中创建CFunctionPointer.相反,您可以通过调用约定(在本例中为c)来注释类型并使用它.直接.

You no longer need to create a CFunctionPointer in Swift 2. Instead, you can annotate your type by calling convention, in this case c, and use it directly.

typealias CFunction = @convention(c) (UnsafeMutablePointer<Void>, Float) -> Int
let bar = unsafeBitCast(foo, CFunction.self)

Swift编程语言@convention描述的相关位. >是:

The relevant bits of the @convention description in the Type Attributes section of The Swift Programming Language are:

c参数用于指示C函数引用.该函数值不包含上下文,并且使用C调用约定.

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

这篇关于Swift 2中的新@convention(c):如何使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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