我尝试构建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)
}
}
}
}
如何解决此问题?
我从here那里学习了这段代码
--------------------更新
编辑Podfile,将Moya更改为pod 'Moya', git: 'https://github.com/Moya/Moya.git', branch: 'development'
在终端中运行pod install
,正在运行。
答案 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)
}