New @convention(c) in Swift 2: How can I use it?

前端 未结 2 1987
南方客
南方客 2020-12-13 09:29

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.

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 10:21

    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, Float) -> Int
    let bar = unsafeBitCast(foo, CFunction.self)
    

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

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

提交回复
热议问题