'Result<Any, AFError>' 类型的值没有成员 'value'

Eli*_*iza 5 xcode ios swift swift5

我正在尝试从 API 获取 JSON 数据,我想在终端上打印它以检查我是否在终端上接收数据,但出现此错误。我目前正在使用 Swift 5 。

import UIKit
import Alamofire
typealias JSON = [String: Any]

class NetworkingService {
    static let shared = NetworkingService()
    private init() {}

    func getPeople(completion: () -> Void) {
        AF.request("https://launchlibrary.net/1.4/launchstatus").responseJSON{ (response) in

            if let json = response.result.value as? JSON { // here I am getting error
                print(json)
            }
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

Yon*_*nat 15

将您的请求完成块替换为:

switch response.result {
case let .success(value):
    print(value)
case let .failure(error):
    print(error)
}
Run Code Online (Sandbox Code Playgroud)

  • 块 `if let json = ... }` (2认同)