Warning Message: How do I remove ‘withUnsafe MutableBytes’ is Deprecated and ‘withUnsafeBytes’ is Deprecated?

I’m changing the code from the previous version of Swift to Swift5. And there is a warning message that this code is not available. I’d like to change this code, but I don’t know how.

warning code

 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
    }

warning Message:

‘withUnsafeMutableBytes’ is deprecated: use withUnsafeMutableBytes<R>(_: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R instead

‘withUnsafeBytes’ is deprecated: use withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R instead

How do I change this code to delete warning message?

I tried many things, but the error changed.

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
    }

Error Message:

Binary operator ‘!=’ cannot be applied to operands of type ‘()’ and ‘Int’

Warning Message:

Constant ‘derivationStatus’ inferred to have type ‘()’, which may be unexpected

Am I right to change it? I think I need to correct the comparison, how am I supposed to correct it?

This Post Has One Comment

  1. No Fault

    I solved this problem with the help of @MartinR. This is the link that @MartinR suggested as an answer.

    Replace saltBytes by saltBytes.bindMemory(to: UInt8.self).baseAddress, and similarly for derivedKeyBytes.

    Successful code with all warnings missing

    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
    }

Leave a Reply