斯威夫特是否不函数指针工作? [英] Does Swift not work with function pointers?

查看:161
本文介绍了斯威夫特是否不函数指针工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想斯威夫特使用C库,和我有麻烦调用,它接受一个函数指针作为它的一个参数的函数。例如,我想在斯威夫特利用lua.h文件的一部分是这样的:

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:

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);

我用桥接报头以获得进入图书馆,并从我的雨燕code我可以叫 lua_setuservalue 没有任何麻烦。但是,如果我尝试调用的 lua_callk 我得到使用未解决的标识符lua_callk'的。如果我从声明中删除函数指针的 lua_callk 后,我不再出现此错误。任何帮助是相当AP preciated。

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.

推荐答案

苹果已经可以作为测试3的函数指针,但它们只能被引用不叫。

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

<一个href=\"https://developer.apple.com/library/$p$prelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-XID_16\">Using斯威夫特与可可和Objective-C

C函数指针被导入到斯威夫特 CFunctionPointer&LT;类型&GT; ,其中键入是一个
  斯威夫特函数类型。例如,具有类型的函数指针 INT(*)(无效)用C
  导入斯威夫特 CFunctionPointer≤() - GT; INT32&GT;

Function Pointers

C function pointers are imported into Swift as CFunctionPointer<Type>, 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>

<一个href=\"http://adcdownload.apple.com//Developer_Tools/x$c$c_6_beta_3_lpw27r/x$c$c_6_beta_3_release_notes__.pdf\">Beta发行说明(PDF)

函数指针现在也进口,并且可以参考与周围通过。然而,
  你不能调用C函数指针或闭合转换为C函数指针类型。

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.

这篇关于斯威夫特是否不函数指针工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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