问题描述
我有Alamofire ResponseSerialization error
,来自xcode的详细控制台日志:
responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.typeMismatch(Swift.Array<Any>,Swift.DecodingError.Context(codingPath: [],debugDescription: "Expected to decode Array<Any> but found a dictionary instead.",underlyingError: nil))))
但是,我的json
数据class
和解析的API request
代码是根据json response
中的API
格式进行格式化和写入的。所以我不明白为什么我得到Alamofire ResponseSerialization error
..?
这是我的response type
中的API
:分别输入dictionary
和Array
:
类型dictionary
:
{
"price": 10194500,"ask": 10195000,"bid": 10184500,"volume": 1752.05558316,"time": "2018-03-14T03:50:41.184Z"
}
然后输入array of dictionaries
:
[
{
"name": "ETH-KRW","baseAsset": "ETH","quoteAsset": "KRW"
},{
"name": "BTC-KRW","baseAsset": "BTC",{
"name": "BCH-KRW","baseAsset": "BCH","quoteAsset": "KRW"
}
]
这是我的API
.GET
请求,将Alamofire
+ PromiseKit
组合在一起:
static func getPairs() -> Promise<[TradingPair]>
let decoder = JSONDecoder()
return Promise { seal in
AF.request(API.TradingPairs,method: .get,parameters: .none,headers: .none).responseDecodable(of: [TradingPair].self,decoder: decoder) { response in
switch response.result {
case .success(let data):
return seal.fulfill(data)
case .failure(let error):
return seal.reject(error)
}
}
}
}
static func getPair(with pairName: String?) -> Promise<TickerByPair> {
let decoder = JSONDecoder()
return Promise { seal in
AF.request(API.getPairByTicker(pairName: pairName!),headers: .none).responseDecodable(of: TickerByPair.self,decoder: decoder) { response in
switch response.result {
case .success(let data):
return seal.fulfill(data)
case .failure(let error):
return seal.reject(error)
}
}
}
}
}
有人可以帮助我理解为什么我仍然得到Alamofire ResponseSerialization error
吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)