小编Gum*_*645的帖子

Swift 并发 UIButton

是否可以在 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)

uikit async-await swift swift-concurrency

2
推荐指数
1
解决办法
1454
查看次数

标签 统计

async-await ×1

swift ×1

swift-concurrency ×1

uikit ×1