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

时间:2019-10-18 06:40:42

标签: ios swift warnings

我将代码从早期版本的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”推断为类型为“()”,可能是   意外

我有权更改它吗?我想我需要纠正比较,我应该如何纠正呢?

1 个答案:

答案 0 :(得分:0)

我在 @MartinR 的帮助下解决了这个问题。 {strong> @MartinR 建议的This is the link作为答案。

saltBytes替换为saltBytes.bindMemory(to: UInt8.self).baseAddress,并类似地替换derivedKeyBytes

成功的代码,缺少所有警告

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
    }