简体   繁体   English

Swift不能用于函数指针吗?

[英]Does Swift not work with function pointers?

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. 我正在尝试在Swift中使用C库,而我在调用任何将函数指针作为其参数之一的函数时遇到了麻烦。 For example, part of the lua.h file that I'm trying to use in Swift looks like this: 例如,我试图在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_setuservalue without any trouble. 我使用桥接头来访问库,从我的Swift代码中我可以毫无困难地调用lua_setuservalue But if I try to call lua_callk I get "use of unresolved identifier 'lua_callk'". 但是,如果我尝试调用lua_callk,我会“使用未解析的标识符'lua_callk'”。 If I remove the function pointer from the declaration for lua_callk , I no longer get this error. 如果我从lua_callk的声明中删除函数指针,我不再收到此错误。 Any help is quite appreciated. 任何帮助都非常感谢。

This answer refers to an earlier version of the Swift language and may no longer be reliable. 这个答案涉及Swift语言的早期版本,可能不再可靠。

While C function pointers are not available in Swift, you can still use swift closures which are passed to C functions as blocks. 虽然在Swift中没有 C函数指针,但仍然可以使用swift闭包,它们作为块传递给C函数。

Doing so requires a few "shim" routines in C to take the block and wrap it in a C function. 这样做需要在C中使用一些“填充”例程来获取块并将其包装在C函数中。 The following demonstrates how it works. 以下演示了它的工作原理。

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. 但这应该给你一个开始。

Apple has made function pointers available as of beta 3, however they can only be referenced not called. 从beta 3开始,Apple已经提供了函数指针,但是它们只能被引用而不被称为。

Using Swift with Cocoa and Objective-C 使用Swift与Cocoa和Objective-C

Function Pointers 功能指针

C function pointers are imported into Swift as CFunctionPointer<Type> , where Type is a Swift function type. C函数指针作为CFunctionPointer<Type>导入Swift,其中Type是Swift函数类型。 For example, a function pointer that has the type int (*)(void) in C is imported into Swift as CFunctionPointer<() -> Int32> 例如,在C中具有int (*)(void)类型的函数指针作为CFunctionPointer<() -> Int32>导入到Swift中

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函数指针类型。

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

Since Swift 3.1 an 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM