swift - 将 UnsafeMutableRawPointer 作为函数参数传递
问题描述
当我尝试将 UnsafeMutableRawPointer 作为函数参数传递时,出现以下错误:
A C function pointer can only be formed from a reference to a 'func' or a literal closure
具体来说,这是来自以下代码:
public func sqlite3_bind_text(_ oP: OpaquePointer!, _ first: Int32, _ second: UnsafePointer<Int8>!, _ third: Int32, _ ptrs: ((UnsafeMutableRawPointer?) -> Void)!) -> Int32 {
SQLite3.sqlite3_bind_text(oP, first, second, third, ptrs)
}
我看过:如何使用实例方法作为函数的回调,该函数只需要 func 或文字闭包,但看不到如何将其应用于我的情况。
如何通过我的函数传递指针?
解决方案
SQLite 是一个纯 C 库,并将SQLite3.sqlite3_bind_text()
指向 C 函数的指针作为第五个参数。这样的参数标有@convention(c)
,并且必须在您的包装函数中复制:
public func sqlite3_bind_text(_ oP: OpaquePointer!, _ first: Int32,
_ second: UnsafePointer<Int8>!,
_ third: Int32,
_ ptrs: (@convention(c) (UnsafeMutableRawPointer?) -> Void)!) -> Int32 {
SQLite3.sqlite3_bind_text(oP, first, second, third, ptrs)
}
推荐阅读
- python - 如何修复 AttributeError:“Mapper”对象没有属性“persist_selectable”
- time - 如何在 vue2-datepicker 中设置时区?
- three.js - 迭代地向一个点螺旋
- angular6 - 当用户在ng2-smart-table中添加数据时单击取消按钮时如何清除所有用户数据?
- javascript - 只允许输入字母或数字
- android - 格式错误的 URL 异常:无协议
- python - 从python中两个数字之间的字典中获取所有键
- laravel - 如何通过标签或 laravel 地平线中的任何内容杀死工作?
- c# - 使用 C# 按相同字符串在列表中出现的频率对列表进行排序
- google-apps-script - 当我复制谷歌表格时脚本停止工作