gpt4 book ai didi

swift - 过滤数据 从 vapor 客户端获取

转载 作者:可可西里 更新时间:2023-11-01 01:57:25 25 4
gpt4 key购买 nike

我正在使用 vapor 的客户端来获取 get 请求。

func sendGetRequest(req: Request) throws -> Future<Response> {
let client = try req.make(FoundationClient.self)
return client.get("http://example.vapor.codes/json", headers: ["Accept-Language" : "ar"])
.map(to: Response.self, { clientResponse in
let response = req.makeResponse()
response.http.status = clientResponse.http.status
response.http.body = clientResponse.http.body
return response
})
}

这会返回所有的 json 数据,我想过滤它只返回 2 个属性,例如在这种情况下 (dict,number)

我已经为数据创建了一个模型

struct ExampleData: Codable {
// var array : [Int]
var dict : [String : String]
var number : Int
// var string : String
}

该函数希望我返回一个 Future ,但如果我将其更改为 Future 并将映射设置为 .map(to: ExampleData.self ..)

我明白了

Cannot convert return expression of type 'Response' to return type 'TodoController.ExampleData'

最佳答案

我想通了

func sendGetRequest(req: Request) throws -> Future<ExampleData> {
let client = try req.make(Client.self)
let ans = client.get("http://example.vapor.codes/json", headers: ["Accept-Language" : "ar"]).flatMap { exampleResponse in
return try exampleResponse.content.decode(ExampleData.self)
}

return ans
}

关于swift - 过滤数据 从 vapor 客户端获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50874767/

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