热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Swift不能与函数指针一起使用吗?-DoesSwiftnotworkwithfunctionpointers?

ImtryingtouseaClibraryinSwift,andImhavingtroublecallinganyfunctionthattakesafun

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_setuservalue without any trouble. But if I try to call lua_callk I 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的声明中删除函数指针,我不再收到此错误。任何帮助都非常感谢。

3 个解决方案

#1


10  

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, where Type is 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函数指针作为CFunctionPointer 导入Swift,其中Type是Swift函数类型。例如,在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函数指针类型。

#2


15  

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.

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

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.

当然,您选择使用闭合装置,以及如何存储和调用它以供使用取决于您。但这应该给你一个开始。

#3


2  

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

在Apple文档中,注意到在Swift中不导入C函数指针。


推荐阅读
author-avatar
手机用户2602932547
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有