Async / AwaitSwift 5.5 為 iOS 15 及更高版本引入了現代并發性,但很快,隨著 Xcode 13.2(以及隨后的 13.2.1)的發布,它使我們能夠使用Async和Await開發 iOS 13 、macOS 10.15 等。但是,當我嘗試發出這樣的異步請求時:
let (data, response) = try await URLSession.shared.data(for: request)
它不能在 iOS 13 上運行。相反,我收到一條錯誤訊息:
data(for:delegate:)僅適用于 iOS 15.0 或更新版本
當我將最低部署目標設定為 iOS 15.0 時,錯誤消失,但我希望該軟體支持 iOS 13.0 。我知道這data(for:delegate:)僅在 iOS 15.0 上受支持,但是如果我無法發出異步網路獲取請求,那么向后兼容 13.0 的意義何在?
data(for:delegate:) 檔案-> 說明最低 iOS 要求是 15.0
Xcode 發行說明-> 在那里他們提到了在 15 之前將 Swift 并發功能部署到 iOS 的 clang 錯誤修復
uj5u.com熱心網友回復:
正如Sundell 的這篇 Swift 文章所述:
盡管 Swift 5.5 的新并發系統在 Xcode 13.2 中變得向后兼容,但一些利用這些新并發功能的內置系統 API 仍然僅在 iOS 15、macOS Monterey 和 Apple 2021 的其余部分可用系統。
這是 John 為一種方法復制 async/await 驅動的 URLSession API 的方式:
@available(iOS, deprecated: 15.0, message: "Use the built-in API instead")
extension URLSession {
func data(from url: URL) async throws -> (Data, URLResponse) {
try await withCheckedThrowingContinuation { continuation in
let task = self.dataTask(with: url) { data, response, error in
guard let data = data, let response = response else {
let error = error ?? URLError(.badServerResponse)
return continuation.resume(throwing: error)
}
continuation.resume(returning: (data, response))
}
task.resume()
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/403686.html
標籤:
