如何使用swift 5
和alamofire 5
解析来自网络请求的XML响应
这是我得到的回应:
<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://www.bnr.ro/xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bnr.ro/xsd nbrfxrates.xsd">
<Header>
<Publisher>National Bank of Romania</Publisher>
<PublishingDate>2020-08-12</PublishingDate>
<MessageType>DR</MessageType>
</Header>
<Body>
<Subject>Reference rates</Subject>
<OrigCurrency>RON</OrigCurrency>
<Cube date="2020-08-12">
<Rate currency="AED">1.1186</Rate>
<Rate currency="AUD">2.9324</Rate>
<Rate currency="BGN">2.4724</Rate>
<Rate currency="BRL">0.7634</Rate>
<Rate currency="CAD">3.0904</Rate>
<Rate currency="CHF">4.4964</Rate>
<Rate currency="CNY">0.5916</Rate>
<Rate currency="CZK">0.1846</Rate>
<Rate currency="DKK">0.6494</Rate>
<Rate currency="EGP">0.2566</Rate>
<Rate currency="EUR">4.8357</Rate>
<Rate currency="GBP">5.3638</Rate>
<Rate currency="HRK">0.6466</Rate>
<Rate currency="HUF" multiplier="100">1.3994</Rate>
<Rate currency="INR">0.0549</Rate>
<Rate currency="JPY" multiplier="100">3.8470</Rate>
<Rate currency="KRW" multiplier="100">0.3470</Rate>
<Rate currency="MDL">0.2478</Rate>
<Rate currency="MXN">0.1842</Rate>
<Rate currency="NOK">0.4582</Rate>
<Rate currency="NZD">2.6941</Rate>
<Rate currency="PLN">1.0966</Rate>
<Rate currency="RSD">0.0411</Rate>
<Rate currency="RUB">0.0562</Rate>
<Rate currency="SEK">0.4710</Rate>
<Rate currency="THB">0.1321</Rate>
<Rate currency="TRY">0.5637</Rate>
<Rate currency="UAH">0.1493</Rate>
<Rate currency="USD">4.1087</Rate>
<Rate currency="XAU">255.0694</Rate>
<Rate currency="XDR">5.7841</Rate>
<Rate currency="ZAR">0.2361</Rate>
</Cube>
</Body>
</DataSet>
我需要的是要保存到对象数组中的货币符号和值
struct CursValutar: Codable {
let curs: [Currency]
}
struct Currency: Codable {
let symbol: String
let value: Double
}
我尝试了一些pod,但没有成功。
发布于 2020-08-14 01:25:09
我建议使用像XMLCoder这样的库,然后可以在Alamofire的responseDecodable
中使用它
// Make XMLDecoder conform to Alamofire's DataDecoder protocol.
extension XMLDecoder: DataDecoder {}
// Then you can use it to decode responses.
AF.request(...).responseDecodable(of: SomeType.self, decoder: XMLDecoder()) { response in
//...
}
https://stackoverflow.com/questions/63396703
复制相似问题