ios - 警告消息:如何删除不推荐使用的“withUnsafeMutableBytes”和不推荐使用的“withUnsafeBytes”?
问题描述
我正在将代码从以前版本的 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' 推断为具有类型 '()',这可能是意料之外的
我改变它是对的吗?我想我需要纠正比较,我应该如何纠正它?
解决方案
我在@MartinR的帮助下解决了这个问题。这是@MartinR建议作为答案的链接。
替换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
}
推荐阅读
- php - 从文本中获取所有大写的“单词”
- agora.io - Agora.io - 支持多少个同时视频通话会话
- sql - SQL 查询以获取过去 12 个月内匹配条件的记录的运行总计
- mysql - 在特定字符串中的列值精确的表中搜索
- python - 将Excel中的数据扫描到python时如何设置自动DataFrame结构?
- c++ - 我想在 C++ 中实现映射,因为我无法检查矢量大小并为我制作的矢量赋值
- python - 为什么 Python venvs 在它们的配置中有一个“home”设置,而它们也有一个 python.exe 文件?
- powerbi - 根据 Power BI 中的开始和结束日期计算活动合同?
- express - 处理 connect-history-api-fallback 和 Express 中的斜杠
- lxml - 您如何获得根元素下的所有内容?