我使用AlamofireObjectMapper扩展。我想得到回应,但它失败了。我的tableLeague对象总是为nil,因为具有响应闭包的代码不会调用({(response:DataResponse)in if 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
}
我认为这可能是因为响应还没有,但我尝试设置我还没有的对象
有问题吗?完成处理程序我需要使用其他吗?
答案 0 :(得分:0)
请尝试按照以下结构进行映射
class Response: Mappable {
var success: Bool?
var data: [Data]?
required init?(_ map: Map) {
mapping(map)
}
func mapping(map: Map) {
success <- map["success"]
data <- map["data"]
}
}
class Data: Mappable {
var uid: Int?
var name: String?
// add other field which you want to map
required init?(_ map: Map) {
mapping(map)
}
func mapping(map: Map) {
uid <- map["uid"]
name <- map["name"]
}
}
当您通过此语法获得JSON响应简单地图时
let response = Mapper<Response>().map(responseObject)
if let id = response?.data?[0].uid {
println(id)
}