从投掷函数到非投掷函数的无效转换

时间:2018-10-24 13:47:26

标签: swift moya

  • xcode:10.0版(10A255)
  • 迅速:4.2
  • 莫亚:4.5

我尝试构建NetworkManager结构。

struct NetworkManager: Networkable {

    internal var provider = MoyaProvider<VideoAPI>(endpointClosure: endpointClosure, requestClosure: requestClosure, plugins: [networkPlugin], trackInflights: false)
    static let environment: APIEnvironment = .development

    func recentlyList(page: Int, completion: @escaping (String) -> (Void)) {
        provider.request(.recentlyList(params: ["page": page])) { (result) in
            switch result {
            case let .success(response):
                do {
                    let jsonData = try JSON(data: response.data)
                    print(jsonData)

                    completion(String(data: response.data, encoding: String.Encoding.utf8)!)
                } catch {
                    print(error)
                }
            case let .failure(error):
                print(error)
            }
        }
    }
}

screenshot

如何解决此问题?

我从here那里学习了这段代码

--------------------更新

编辑Podfile,将Moya更改为pod 'Moya', git: 'https://github.com/Moya/Moya.git', branch: 'development'

在终端中运行pod install,正在运行。

1 个答案:

答案 0 :(得分:1)

您的代码中没有catch,这意味着您没有捕获到解析错误,从而使您的闭包抛出

添加catch应该可以解决此问题:

do {
    let jsonData = try JSON(data: response.data)
    print(jsonData)

    completion(String(data: response.data, endcoding: String.Encoding.utf8)!)
} catch { 
    print(error)
}