gpt4 book ai didi

ios - 警告消息 : How do I remove 'withUnsafeMutableBytes' is deprecated and 'withUnsafeBytes' is deprecated?

转载 作者:行者123 更新时间:2023-11-29 05:23:22 32 4
gpt4 key购买 nike

我正在将代码从以前版本的 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' is deprecated: use withUnsafeMutableBytes<R>(_: (UnsafeMutableRawBufferPointer) throws
-> R) rethrows -> R
instead

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

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

我尝试了很多方法,但错误发生了变化。

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
}

错误信息:

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

警告消息:

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

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

最佳答案

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

saltBytes 替换为 saltBytes.bindMemory(to: UInt8.self).baseAddress,对于 衍生 key 字节 也类似。

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

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
}

关于ios - 警告消息 : How do I remove 'withUnsafeMutableBytes' is deprecated and 'withUnsafeBytes' is deprecated?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58445258/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com