Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向Swift 5.1和x27发布更新代码;withUnsafeBytes不推荐使用:使用withUnsafeBytes(:(UnsafeRawBufferPointer)抛出->;R)重试->;R代替';_Swift_Deprecated_Swift5_Unsafe Pointers - Fatal编程技术网

向Swift 5.1和x27发布更新代码;withUnsafeBytes不推荐使用:使用withUnsafeBytes(:(UnsafeRawBufferPointer)抛出->;R)重试->;R代替';

向Swift 5.1和x27发布更新代码;withUnsafeBytes不推荐使用:使用withUnsafeBytes(:(UnsafeRawBufferPointer)抛出->;R)重试->;R代替';,swift,deprecated,swift5,unsafe-pointers,Swift,Deprecated,Swift5,Unsafe Pointers,我正在使用一个简单的扩展来打包和解包数据,下面的代码目前仍然有效,但它给了我一个警告,我想对它进行更新,以防止出现问题 关于这一点,有两个类似的问题被问到,但我还不能成功地应用那里讨论的内容。所以我希望有人能给我一些启示 发出警告的分机: extension Data { var unsafeBytes : UnsafePointer<UInt8> { return self.withUnsafeBytes { return $0 } //'withUnsaf

我正在使用一个简单的扩展来打包和解包数据,下面的代码目前仍然有效,但它给了我一个警告,我想对它进行更新,以防止出现问题

关于这一点,有两个类似的问题被问到,但我还不能成功地应用那里讨论的内容。所以我希望有人能给我一些启示

发出警告的分机:

extension Data {
    var unsafeBytes : UnsafePointer<UInt8> {
        return self.withUnsafeBytes { return $0 } //'withUnsafeBytes' is deprecated: use `withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R` instead
    }
}
扩展数据{
var unsafeBytes:UnsafePointer{
return self.withUnsafeBytes{return$0}/'withUnsafeBytes'已被弃用:改用`withUnsafeBytes(u:(UnsafeRawBufferPointer)throws->R)rethrrows->R`
}
}
使用它的函数之一:

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)
    }
func upack(\bin:Data)->Data{
让unsafeBin=bin.unsafeBytes
let output=UnsafeMutablePointer.allocate(容量:540)
如果(!nfc_upack(键、解除安全、输出)){
日志。警告(“解包失败”)
}
返回数据(字节:输出,计数:540)
}
编辑:
nfc\u upack
导致


正如我所说,目前它运行良好,但我希望在它成为问题之前解决它。

因此,要解决此警告,实际的实现方式如下:

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\u upack
api共享的输出布尔值。
然后,您可以检查状态是否为true,然后输出参数将具有有效值。

您可以共享nfc_upack的api,它的参数是什么。@AnkitThakur它引用了这个GitHub文件中的第73行(唯一的区别是名称):函数是包含
var键的结构的一部分:UnsafeMutablePointer=UnsafeMutablePointer.allocate(容量:1)