我使用一个简单的扩展来打包和解压缩数据,下面的代码目前仍然有效,但是它给了我一个警告,我想要更新它,以防止出现问题。
关于这一点,有两个类似的问题,但我还未能成功地应用已经讨论过的内容。所以我希望有人能给我一些见解。
抛出警告的分机:
extension Data {
var unsafeBytes : UnsafePointer<UInt8> {
return self.withUnsafeBytes { return $0 } //'withUnsafeBytes' is deprecated: use `withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R` instead
}
}
使用它的功能之一是:
func upack(_ bin: Data) -> Data {
let unsafeBin = bin.unsafeBytes
let output = UnsafeMutablePointer<UInt8>.allocate(capacity: 540)
if (!nfc_upack(key, unsafeBin, output)) {
log.warning("Unpacking failed")
}
return Data(bytes: output, count: 540)
}
编辑:nfc_upack
通向https://github.com/socram8888/amiitool/blob/master/amiibo.c#L73
就像我说的,目前它运行良好,但我想在它成为一个问题之前解决这个问题。
发布于 2020-07-07 07:30:13
因此,要解决这一警告,实际的实现方式如下:
let data = Data()
var output = Data(count: 540)
var satus:Bool = false
satus = output.withUnsafeMutableBytes { outputBytes in
data.withUnsafeBytes { dataBytes in
nfc_upack(key, unsafeBin, output)
}
}
status
是nfc_upack
api共享的输出bool值。然后,您可以检查状态是否为真,然后输出参数将具有有效值。
https://stackoverflow.com/questions/62760844
复制相似问题