gpt4 book ai didi

swift - Alamofire.request().responseObject 不返回响应

转载 作者:行者123 更新时间:2023-11-30 12:48:14 25 4
gpt4 key购买 nike

我使用 AlamofireObjectMapper 扩展。我想得到回应,但失败了。我的 tableLeague 对象始终为零,因为具有响应闭包的代码不会调用 ({ (response: DataResponse) 如果响应.结果.isSuccess { tableLeague = 响应.结果.值 } 不调用)。我使用以下方法:

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
}

我认为这可能是因为尚未响应,但我尝试设置尚未设置的对象

有什么问题吗?我需要使用其他完成处理程序吗?

最佳答案

请尝试按以下结构进行映射

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)
}

关于swift - Alamofire.request().responseObject 不返回响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41341408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com