【发布时间】:2016-12-27 08:12:06
【问题描述】:
我使用 AlamofireObjectMapper 扩展。我想得到回应,但它失败了。我的 tableLeague 对象始终为零,因为带有响应闭包的代码不会调用 ({ (response: DataResponse) in 如果 response.result.isSuccess { tableLeague = response.result.value } 不调用)。我使用下一个方法:
let header = ["X-Auth-Token":"1231231"]
static let sharedInstance = ServerAPI()
private func getRequest(uri: String) -> DataRequest {
return Alamofire.request(uri, method: .get, headers: header)
}
public func getTableLeague() -> TableLeague {
var tableLeague: TableLeague?
getRequest(uri: URL).responseObject { (response: DataResponse<TableLeague>) in
if response.result.isSuccess {
tableLeague = response.result.value
}
}
return tableLeague!
}
并在商务舱中使用:
public func readTableLeague() -> TableLeague {
let tableLeague = ServerAPI.sharedInstance.getTableLeague()
return tableLeague
}
我认为这可能是因为尚未响应,但我尝试设置尚未设置的对象
有什么问题吗?我需要使用其他的完成处理程序吗?
【问题讨论】:
-
请添加更多关于您的问题的描述。
-
我添加更多描述。
-
这意味着您的 Mapper 类不会将响应映射到您的 TableLeague 类。请检查响应对象是否包含 JSON 对象。
-
是json对象
-
{ "_links":{ "self":{ "href":"api.football-data.org/v1/competitions/426/leagueTable/…" }, "competition":{ "href":"api.football-data.org/v1/competitions/426" } }, "leagueCaption" :"Premier League 2016/17", "matchday":18, "standing":[ {...} }
标签: swift request response alamofire objectmapper