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?