我有一个生成挑战字符串的代码。
private func codeChallenge(for verifier: String) -> String {
guard let data = verifier.data(using: .utf8) else { fatalError() }
var buffer = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA256($0, CC_LONG(data.count), &buffer)
}
let hash = Data(buffer)
return hash.base64EncodedString()
.replacingOccurrences(of: "+", with: "-")
.replacingOccurrences(of: "/", with: "_")
.replacingOccurrences(of: "=", with: "")
.trimmingCharacters(in: .whitespaces)
}它按预期工作,但是它会产生警告:
'withUnsafeBytes' is deprecated: use `withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R` instead我不确定我是否疯了,但警告再次引用了废弃的方法作为解决方案。我是不是遗漏了什么?
发布于 2022-03-26 15:21:03
,我是不是遗漏了什么?
是。被反对的是
func withUnsafeBytes<ResultType, ContentType>(_ body: (UnsafePointer<ContentType>) throws -> ResultType) rethrows -> ResultType替换者是
func withUnsafeBytes<ResultType>(_ body: (UnsafeRawBufferPointer) throws -> ResultType) rethrows -> ResultType这两种方法中可能都有"withUnsafeBytes“一词,但这基本上是无关紧要的;它们是完全不同的方法,有着完全不同的签名。
https://stackoverflow.com/questions/71629267
复制相似问题