警告消息:如何删除已弃用的“withUnsafeMutableBytes”和已弃用的“withUnsafeBytes”?

Warning Message: How do I remove 'withUnsafeMutableBytes' is deprecated and 'withUnsafeBytes' is deprecated?

提问人:iosbegindevel 提问时间:10/18/2019 最后编辑:iosbegindevel 更新时间:10/18/2019 访问量:270

问:

我正在将代码从以前的 Swift 版本更改为 Swift5。并且有一条警告消息,指出此代码不可用。我想更改此代码,但我不知道如何更改。

警告代码

 func pbkdf2(hash: CCPBKDFAlgorithm, password: String, salt: Data, keyByteCount: Int, round: Int) -> Data? {
        let passwordData = password.data(using: .utf8)!
        let derivedKeyData = Data(count: keyByteCount)
        var localVariables = derivedKeyData
        let derivationStatus = localVariables.withUnsafeMutableBytes { derivedKeyBytes  in
            salt.withUnsafeBytes { saltBytes in
                CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2),
                                     password, passwordData.count, saltBytes, salt.count,
                                     hash, UInt32(round),
                                     derivedKeyBytes, derivedKeyData.count)
            }
        }

        if (derivationStatus != 0) {
            Log.Error("\(derivationStatus)")
            return nil;
        }

        return localVariables
    }

警告消息:

“withUnsafeMutableBytes”已弃用:请改用withUnsafeMutableBytes<R>(_: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R

“withUnsafeBytes”已弃用:请改用withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R

如何更改此代码以删除警告消息?

我尝试了很多东西,但错误发生了变化。

func pbkdf2(hash: CCPBKDFAlgorithm, password: String, salt: Data, keyByteCount: Int, round: Int) -> Data? {
  let passwordData = password.data(using: .utf8)!
  let derivedKeyData = Data(count: keyByteCount)
  var localVariables = derivedKeyData
  let derivationStatus = localVariables.withUnsafeMutableBytes { derivedKeyBytes  in
     let Mutable: UnsafeMutableRawPointer? = derivedKeyBytes.baseAddress
     salt.withUnsafeBytes { saltBytes in
           let raw: UnsafeRawPointer? = saltBytes.baseAddress
              CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2),
                                     password, passwordData.count, raw?.assumingMemoryBound(to: UInt8.self), salt.count,
                                     hash, UInt32(round),
                                     Mutable?.assumingMemoryBound(to: UInt8.self) , derivedKeyData.count)
            }
        }

        if (derivationStatus != 0) {
            Log.Error("\(derivationStatus)")
            return nil;
        }

        return localVariables
    }

错误信息:

二进制运算符“!=”不能应用于类型为“()”和“Int”的操作数

警告消息:

常量“derivationStatus”推断为类型“()”,其类型可能是 意外

我改变它对吗?我想我需要纠正比较,我应该如何纠正它?

iOS Swift 警告

评论

0赞 Martin R 10/18/2019
stackoverflow.com/a/55499502/1187415 有帮助吗?– 这个问题与 [r] 有什么关系?
0赞 iosbegindevel 10/18/2019
我添加它是因为警报有一个 R。而且,我已经查看了您的链接,但我不知道如何更改我的代码......我对 swift 了解不多。你能帮我@MartinR吗?
0赞 Martin R 10/18/2019
r 标签用于询问有关 R 的问题,R 是“用于统计计算和图形的自由软件环境”。
0赞 iosbegindevel 10/18/2019
哦,@MartinR我不知道它是否与此不同。对不起。
0赞 iosbegindevel 10/18/2019
@MartinR 如何删除警告?我需要你的帮助。

答:

0赞 iosbegindevel 10/18/2019 #1

我在@MartinR的帮助下解决了这个问题。这是@MartinR建议作为答案的链接。

替换为 ,类似地替换为 。saltBytessaltBytes.bindMemory(to: UInt8.self).baseAddressderivedKeyBytes

缺少所有警告的成功代码

func pbkdf2(hash: CCPBKDFAlgorithm, password: String, salt: Data, keyByteCount: Int, round: Int) -> Data? {
    let passwordData = password.data(using: .utf8)!
    let derivedKeyData = Data(count: keyByteCount)
    var localVariables = derivedKeyData
    let derivationStatus = localVariables.withUnsafeMutableBytes { derivedKeyBytes  in
      salt.withUnsafeBytes { saltBytes in

       CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2),
                          password, passwordData.count, saltBytes.bindMemory(to: UInt8.self).baseAddress, salt.count,
                          hash, UInt32(round),
                          derivedKeyBytes.bindMemory(to: UInt8.self).baseAddress , derivedKeyData.count)
            }
        }

        if (derivationStatus != 0) {
                 Log.Error("\(derivationStatus)")
                 return nil;
        }

        return localVariables
    }

评论

0赞 iosbegindevel 10/18/2019
@MartinR嗯......我还有一个问题。UInt8 的范围从 0 到 255。我认为这是一个非常小的范围。UInt8 具有重复值的可能性是否较小?