是否可以在 UIKit 中使用 async/await 进行 POST HTTP 请求,即 from UIButton?
我可以发送请求,但它立即崩溃。完美地使用URLSession,并以此作为学习经验。
lazy var buttonTest: UIButton = {
let button = UIButton()
button.addTarget(self, action: #selector(testAsyncTwo), for: .touchUpInside)
return button
}()
@objc func testAsync() async throws {
let date = Date()
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = df.string(from: date)
let json: [String: Any] = ["item": "1",
"time": "\(dateString)"]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
guard let url = URL(string: "https://myapiendpoint.com/api/") else { fatalError("Missing URL") …Run Code Online (Sandbox Code Playgroud)