如果我有:如何将字段添加到Moya.Response JSON,这是不是在真正的有效载荷从HTTP响应
import Moya
import RxSwift
import ObjectMapper
import Moya_ObjectMapper
provider.request(.callApi(id: id))
.mapObject(Thing.self)
.subscribeOn(ConcurrentDispatchQueueScheduler(qos: .background))
.observeOn(MainScheduler.instance)
...
struct Thing: Mappable, Equatable {
var id: String?
init?(map: Map) {
}
mutating func mapping(map: Map) {
id <- map["id"]
}
发送HTTP api调用并返回类似于{“id:”123“}的json,并且它的运行效果很好,一个新的Thing结构是用正确的id创建的,但是如果我想为Thing添加”flavor“并且硬编码{”id :“123”,“味道”:“某物”}。
即我们只需修改实际的http响应主体并在它到达.mapObject方法之前添加"flavor": "something"
。哪里是正确的地方挖掘呢?
这不仅仅是将它添加到Thing中的映射函数中,因为每个id的“something”是不同的。可能是味道:“东西1”,然后味道:“东西2”。我在相同的范围内callApi这个值(ID:ID),所以像:
provider.request(.callApi(id: id))
.addJSON("flavor", flavor)
.mapObject(Thing.self)
.subscribeOn(ConcurrentDispatchQueueScheduler(qos: .background))
.observeOn(MainScheduler.instance)
但.addJSON
是我编造的。它不存在。但是必须有一些简单的解决方案呢?
最终直接与Alamofire进行每个http调用,然后很简单。如果有人很好奇,这是所有https://github.com/andrewarrow/boring_company_chat。 –