xcode Swift 不能使用函数指针吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24088312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:01:40  来源:igfitidea点击:

Does Swift not work with function pointers?

ioscxcodeluaswift

提问by Mike

I'm trying to use a C library in Swift, and I'm having trouble calling any function that takes a function pointer as one of it's arguments. For example, part of the lua.h file that I'm trying to use in Swift looks like this:

我正在尝试在 Swift 中使用 C 库,但在调用任何将函数指针作为参数之一的函数时遇到问题。例如,我尝试在 Swift 中使用的 lua.h 文件的一部分如下所示:

LUA_API void  (lua_setuservalue) (lua_State *L, int idx);


typedef int (*lua_CFunction) (lua_State *L);

LUA_API void  (lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
                       lua_CFunction k);

I use the bridging header to get access to the library, and from my Swift code I can call lua_setuservaluewithout any trouble. But if I try to call lua_callkI get "use of unresolved identifier 'lua_callk'". If I remove the function pointer from the declaration for lua_callk, I no longer get this error. Any help is quite appreciated.

我使用桥接头来访问库,并且从我的 Swift 代码中,我可以毫无困难地调用lua_setuservalue。但是,如果我尝试调用lua_callk,我会得到“使用未解析的标识符‘lua_callk’”。如果我从lua_callk的声明中删除函数指针,则不会再出现此错误。非常感谢任何帮助。

回答by Jim Hayes

This answer refers to an earlier version of the Swift language and may no longer be reliable.

这个答案指的是早期版本的 Swift 语言,可能不再可靠。

While C function pointers are notavailable in Swift, you can still use swift closures which are passed to C functions as blocks.

虽然 C 函数指针在 Swift中不可用,但您仍然可以使用作为块传递给 C 函数的 swift 闭包。

Doing so requires a few "shim" routines in C to take the block and wrap it in a C function. The following demonstrates how it works.

这样做需要在 C 中使用一些“填充”例程来获取块并将其包装在 C 函数中。下面演示了它是如何工作的。

Swift:

迅速:

func foo(myInt: CInt) -> CInt {
    return myInt
}

var closure: (CInt) -> CInt = foo;

my_c_function(closure)

C:

C:

void my_c_function(int (^closure)(int))
{
    int x = closure(10);
    printf("x is %d\n", x);
}

Of course what you choose to do with the closure, and how you store and recall it for use is up to you. But this should give you a start.

当然,您选择如何处理封盖,以及如何储存和召回以供使用取决于您。但这应该给你一个开始。

回答by TiLogic

Apple has made function pointers available as of beta 3, however they can only be referenced not called.

Apple 从 beta 3 开始提供函数指针,但是它们只能被引用不能被调用。

Using Swift with Cocoa and Objective-C

将 Swift 与 Cocoa 和 Objective-C 结合使用

Function Pointers

C function pointers are imported into Swift as CFunctionPointer<Type>, where Typeis a Swift function type. For example, a function pointer that has the type int (*)(void)in C is imported into Swift as CFunctionPointer<() -> Int32>

函数指针

C 函数指针作为 导入到 Swift 中CFunctionPointer<Type>,其中Type是 Swift 函数类型。例如,一个具有int (*)(void)C 中类型的函数指针被导入到 Swift 中作为CFunctionPointer<() -> Int32>

Beta 3 Release Notes(PDF)

Beta 3 发行说明(PDF)

Function pointers are also imported now, and can be referenced and passed around. However, you cannot call a C function pointer or convert a closure to C function pointer type.

函数指针现在也被导入,并且可以被引用和传递。但是,您不能调用 C 函数指针或将闭包转换为 C 函数指针类型。

回答by Antarr Byrd

In the Apple documentationit is noted that C function pointers are not imported in Swift.

在 Apple文档中,注意到C function pointers are not imported in Swift.

回答by Kamil.S

Since Swift 3.1an arbitrary address being a function pointer can be called like this:

Swift 3.1 开始,可以像这样调用任意地址作为函数指针:

let fakeIMP = unsafeBitCast(0x1e233d1d0, to: IMP.self)
unsafeBitCast(fakeIMP,to:(@convention(c)()->Void).self)()

Assuming for this^ particular call signature:

假设对于这个^特定的调用签名:

void cFunction();