简体   繁体   中英

Invalid conversion from throwing function to non-throwing function

  • xcode: Version 10.0 (10A255)
  • swift: 4.2
  • Moya: 4.5

I try to make NetworkManager struct.

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)
            }
        }
    }
}

截图

How can i fix this issue?

I learn this code from here

--------------------update

Edit Podfile, change Moya to pod 'Moya', git: 'https://github.com/Moya/Moya.git', branch: 'development'

run pod install in terminal, It's working.

There is no catch in your code which means that your are not catching the parsing errors, making your closure throwing .

Adding catch should fix the problem:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM