【发布时间】:2019-05-02 15:09:59
【问题描述】:
我有一个使用 SwiftyJSON 并且可以工作的应用程序。但是,我现在想扩展项目并重构代码,但我遇到了一些问题,因为我现在切换到 Codable,我需要能够从任何路径而不是硬编码路径映射 JSON。目前我的 jsonResponse 看起来像这样
/// handle the network response and map to JSON
/// - returns: Observable<JSON>
func handleResponseMapJSON() -> Observable<Result<JSON, ORMError>> {
return self.map { representor in
guard let response = representor as? Moya.Response else {
return .failure(ORMError.ORMNoRepresentor)
}
guard ((200...299) ~= response.statusCode) else {
return .failure(ORMError.ORMNotSuccessfulHTTP)
}
guard let json = JSON.init(rawValue: response.data),
json != JSON.null,
let code = json["code"].int else {
return .failure(ORMError.ORMParseJSONError)
}
guard code == BizStatus.BizSuccess.rawValue else {
let message: String = {
let json = JSON.init(data: response.data)
guard let msg = json["status"].string else { return "" }
return msg
}()
log(message, .error)
return .failure(ORMError.ORMBizError(resultCode: "\(code)", resultMsg: message))
}
return .success(json["result"])
}
}
如何消除硬编码json[""] 值的通道。任何帮助表示赞赏
【问题讨论】:
标签: swift rx-swift swifty-json codable moya