我有一個生成挑戰字串的代碼。
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
我不確定我是否瘋了,但警告再次參考不推薦使用的方法作為解決方案。我錯過了什么嗎?
uj5u.com熱心網友回復:
我錯過了什么嗎?
是的。被棄用的是
func withUnsafeBytes<ResultType, ContentType>(_ body: (UnsafePointer<ContentType>) throws -> ResultType) rethrows -> ResultType
替換是
func withUnsafeBytes<ResultType>(_ body: (UnsafeRawBufferPointer) throws -> ResultType) rethrows -> ResultType
這兩種方法中可能都有“withUnsafeBytes”這個短語,但這基本上是無關緊要的;它們是完全不同的方法,具有完全不同的簽名。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/450998.html
