【发布时间】:2019-08-21 17:27:24
【问题描述】:
我有一个使用外部提供的 c 库快速解析 FIT 文件的库。解析函数将void * data 作为参数。
为了调用该函数,我使用 data.withUnsafeBytes( { (ptr: UnsafePointer<UInt8>) in ...} 转换数据以构建 c 函数的参数,它工作正常。
Xcode 升级到 swift 5 后,我现在收到一个弃用的警告
'withUnsafeBytes' 已弃用:改用withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R
我不知道如何修复代码以删除已弃用的警告。代码一直运行良好,在 swift 4 中没有警告
我尝试将闭包中的参数更改为采用 UnsafeRawBufferPointer 而不是 UnsafePointer,但这导致调用函数时出错:Cannot convert 'UnsafeRawBufferPointer' to expected argument type 'UnsafeRawPointer?'
这是一个显示问题的小 swift 文件:
import Foundation
// Create sample data (Typically would be read from a file
let data = Data(repeating: 1, count: 10)
data.withUnsafeBytes( { (ptr : UnsafePointer<UInt8>) in
// call the c function with the void* argument
let value = readFITfile(ptr)
print( value )
})
还有一个例子c函数
unsigned readFITfile(const void * data){
//Silly example to show it works, just returning the value of pointer as int
//Typically would parse the data and return a structure
return (unsigned)data;
}
我用上面的代码在https://github.com/roznet/swift2c 保存了一个小仓库,解析文件的完整项目在这里https://github.com/roznet/fit-sdk-swift
【问题讨论】:
标签: c swift unsafe-pointers