'withUnsafeBytes' is deprecated warning when passing void* argument to c function in swift 5
'withUnsafeBytes' is deprecated warning when passing void* argument to c function in swift 5
我在 swift 中有一个使用外部提供的 c 库解析 FIT 文件的库。解析函数将 a 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 and the full scale project with the parsing of the file is here https://github.com/roznet/fit-sdk-swift
您必须将闭包参数更改为 UnsafeRawBufferPointer
,然后取其 baseAdress
(这是一个 UnsafeRawPointer?
,相当于 void *
的 Swift在 C):
data.withUnsafeBytes( { (ptr : UnsafeRawBufferPointer) in
let value = readFITfile(ptr.baseAddress)
// ...
})
Swift 编译器还可以自动推断闭包参数类型:
data.withUnsafeBytes( { ptr in
let value = readFITfile(ptr.baseAddress)
// ...
})
有关此问题的详细信息,请参阅 Swift 论坛中的 withUnsafeBytes Data API confusion。
现在要获取 UnsafePointer,您应该这样做
data.withUnsafeBytes { (ptr: UnsafeRawBufferPointer) in
if let ptrAddress = ptr.baseAddress, ptr.count > 0 {
let pointer = ptrAddress.assumingMemoryBound(to: UInt8.self) // here you got UnsafePointer<UInt8>
let value = readFITfile(ptr)
print( value )
} else {
// Here you should provide some error handling if you want ofc
}
}
我在 swift 中有一个使用外部提供的 c 库解析 FIT 文件的库。解析函数将 a 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 and the full scale project with the parsing of the file is here https://github.com/roznet/fit-sdk-swift
您必须将闭包参数更改为 UnsafeRawBufferPointer
,然后取其 baseAdress
(这是一个 UnsafeRawPointer?
,相当于 void *
的 Swift在 C):
data.withUnsafeBytes( { (ptr : UnsafeRawBufferPointer) in
let value = readFITfile(ptr.baseAddress)
// ...
})
Swift 编译器还可以自动推断闭包参数类型:
data.withUnsafeBytes( { ptr in
let value = readFITfile(ptr.baseAddress)
// ...
})
有关此问题的详细信息,请参阅 Swift 论坛中的 withUnsafeBytes Data API confusion。
现在要获取 UnsafePointer,您应该这样做
data.withUnsafeBytes { (ptr: UnsafeRawBufferPointer) in
if let ptrAddress = ptr.baseAddress, ptr.count > 0 {
let pointer = ptrAddress.assumingMemoryBound(to: UInt8.self) // here you got UnsafePointer<UInt8>
let value = readFITfile(ptr)
print( value )
} else {
// Here you should provide some error handling if you want ofc
}
}