swift - 如何处理`withUnsafeBytes`被弃用的警告?
问题描述
我收到以下代码的弃用警告,
_ = data.withUnsafeBytes {
_ = CC_SHA256($0, CC_LONG(data.count), &digest)
}
'withUnsafeBytes' 已弃用:
withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R
改用
我该如何处理?
更新
正如 Martin 所建议的,我在Swift-5 中使用了以下代码,
func sha256(data : Data) -> Data {
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA256($0.baseAddress, CC_LONG(data.count), &hash)
}
return Data(hash)
}
这甚至给编译器错误,
“UnsafePointer<_>”类型的值没有成员“baseAddress”
更新2
解决方案
Martin的解决方案适用Xcode 11.3.1+
于
func sha256(data : Data) -> Data {
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA256($0.baseAddress, CC_LONG(data.count), &hash)
}
return Data(hash)
}
推荐阅读
- python - 带有熊猫条形图的 axvline 使用句点
- javascript - 如何检查数组是否包含 Javascript 中 JSON 数据的值?
- node.js - 如何在nodejs中使函数同步
- reactjs - 当我在 redux saga 中提交表单数据时有时会返回成功有时不会
- flutter - Flutter:在小部件测试中未调用 Mapbox onMapCreated
- c# - CosmosDB 文档客户端 - 如何为查询隐式添加类型?
- little-man-computer - 对输入数字求和的程序不起作用
- r - 在无状态(谷歌云引擎)docker上保留来自R的永久日志的最佳方法是什么?
- php - 仅在结帐时禁用特定运输方式的支付网关
- amazon-web-services - 具有固定 IP 的 AWS 客户端 VPN