ios – Swift不能与函数指针一起使用吗?

ios – Swift不能与函数指针一起使用吗?,第1张

概述我正在尝试在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 ( 我正在尝试在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);

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

解决方法 从beta 3开始,Apple已经提供了函数指针,但是它们只能被引用而不能被调用.

Using Swift with Cocoa and Objective-C

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>

Beta 3 Release Notes(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.

总结

以上是内存溢出为你收集整理的ios – Swift不能与函数指针一起使用吗?全部内容,希望文章能够帮你解决ios – Swift不能与函数指针一起使用吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1066882.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存