我的目标是使用标准Moya请求来设置completion handler
。
这是我的流程:
MoyaProvider
(已设置)的BackendAPI
来呼叫后端[Player]
数据(Player
是项目中的自定义类)[Player]
数据以下是实际代码:
func getPlayers(orchestraId: String, finished: @escaping () -> [Player]) {
let provider = MoyaProvider<BackendAPI>()
provider.request(.getPlayers(orchestraId: orchestraId)) { (result) in
switch result {
case let .success(moyaResponse):
let statusCode = moyaResponse.statusCode
if statusCode == 200 {
let data = moyaResponse.data
let json = JSON.init(data: data)
let players: [Player] = self.deserializeJSONPlayers(with: json)
return players
} else {
print ("Non 200 for league players data")
self.debugStatementsFromResponse(response: moyaResponse)
}
case let .failure(error):
print ("Error: \(error)")
}
}
}
我在return
行收到错误,注意到Unexpected non-void
返回void函数。但是,我已将我的函数声明为non-void
函数。我在构建方法时做错了什么?
答案 0 :(得分:1)
如果要使用当前语法返回值,则应使用completionHandler
。如果您想使用return players
,则必须更改语法。
请使用此语法使您的当前代码与completionHandler
:
func getPlayers(orchestraId: String, finished: @escaping ([Player]) -> Void) {
finished(players) // Instead of return players
}