加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 01:44:33 所属栏目:百科 来源:网络整理
导读:我正在尝试在Swift中使用C库,而我在调用任何将函数指针作为其参数之一的函数时遇到了麻烦.例如,我试图在Swift中使用的lua.h文件的一部分如下所示: LUA_API void (lua_setuservalue) (lua_State *L,int idx);typedef int (*lua_CFunction) (lua_State *L);LU
我正在尝试在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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读