Swift4 错误无法将类型 '(_) -> Void' 的值转换为预期的参数类型 '(_) -> _'
Swift4 Error Cannot convert value of type '(_) -> Void' to expected argument type '(_) -> _'
使用 PromiseKit API 调用 Swift 4:
let apiCall = ApiManager.shared.fetchActors()
apiCall.then { actors -> Void in
self.dataSourceArray = actors
self.tableView.reloadData()
}.catch { error -> Void in
}
我收到这个错误:
Cannot convert value of type '() -> Void' to expected argument type '() -> _'
如何解决这个问题?
将.catch { error -> Void in
替换为.catch { error in
解决方案:
func fetchActorsFromApi () -> Promise<[Actor]> {
return Promise<[Actor]> { seal in
return Alamofire.request(API_URL).validate().responseString(completionHandler: {
response in
switch (response.result) {
case .success(let responseString1):
print (responseString1) // should be converted into the modelclass
let actorResponse = ActorApiResponse(JSONString: "\(responseString1)")!
seal.fulfill(actorResponse.actors!)
case .failure(let error):
print (error)
seal.reject(error)
}
})
}
}
let apiCall = ApiManager.shared.fetchActorFromAPI()
let _ = apiCall.done {
actors -> Void in
self.dataSource = actors
self.myTableView.reloadData()
}.catch {
error -> Void in
}
使用 this.In 新版本的 PromiseKit .done() 函数可以做到这一点。文档:https://github.com/mxcl/PromiseKit
使用 .done 而不是使用 .then 它将起作用。
使用 PromiseKit API 调用 Swift 4:
let apiCall = ApiManager.shared.fetchActors()
apiCall.then { actors -> Void in
self.dataSourceArray = actors
self.tableView.reloadData()
}.catch { error -> Void in
}
我收到这个错误:
Cannot convert value of type '() -> Void' to expected argument type '() -> _'
如何解决这个问题?
将.catch { error -> Void in
替换为.catch { error in
解决方案:
func fetchActorsFromApi () -> Promise<[Actor]> {
return Promise<[Actor]> { seal in
return Alamofire.request(API_URL).validate().responseString(completionHandler: {
response in
switch (response.result) {
case .success(let responseString1):
print (responseString1) // should be converted into the modelclass
let actorResponse = ActorApiResponse(JSONString: "\(responseString1)")!
seal.fulfill(actorResponse.actors!)
case .failure(let error):
print (error)
seal.reject(error)
}
})
}
}
let apiCall = ApiManager.shared.fetchActorFromAPI()
let _ = apiCall.done {
actors -> Void in
self.dataSource = actors
self.myTableView.reloadData()
}.catch {
error -> Void in
}
使用 this.In 新版本的 PromiseKit .done() 函数可以做到这一点。文档:https://github.com/mxcl/PromiseKit
使用 .done 而不是使用 .then 它将起作用。