我从城市的天气提供商那里以XML格式获取警告数据。我正在使用SWXMLHash对其进行解析。在迭代项目时。它将显示某种错误:
2019-03-27 20:50:03.614909+0800 Hong Kong Weather[2665:397304] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C1.1:2][0x7f84a8e284d0] get output frames failed, state 8196
2019-03-27 20:50:03.615344+0800 Hong Kong Weather[2665:397304] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C1.1:2][0x7f84a8e284d0] get output frames failed, state 8196
我在print语句之前设置了一个断点,并检查了元素变量,它包含以下内容:
Printing description of elem:
expression produced error: error: /var/folders/bn/yg45nq716v353dst4mggsq_00000gn/T/expr44-1b81c1..swift:1:75: error: 'XMLIndexer' is not a member type of 'SWXMLHash'
Swift._DebuggerSupport.stringForPrintObject(Swift.UnsafePointer<SWXMLHash.XMLIndexer>(bitPattern: 0x12c9d88b0)!.pointee)
~~~~~~~~~ ^
我想这个错误的重点是“ XMLIndexer不是SWXMLHash的成员类型”,由于我是Swift的新手,所以我不能很好地理解它。我最近迁移到了Swift5。我尝试回到Swift 4.2,然后回到Swift 4,但这没有帮助。
请求和解析代码
Alamofire.request("https://web.archive.org/web/20180806223212if_/http://rss.weather.gov.hk:80/rss/WeatherWarningSummaryv2.xml")
.responseData { response in
if let data = response.data {
let dataString = NSString(data: data, encoding: String.Encoding.utf8.rawValue)?.replacingOccurrences(of: "utf-16", with: "utf-8")
let xml = SWXMLHash.config {
config in
config.shouldProcessLazily = false
}.parse(dataString!)
for elem in xml["rss"]["channel"]["item"].all {
print(elem.element!.text)
}
}
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="style.xsl" type="text/xsl"?>
<rss version="2.0">
<channel>
<title>Weather Warning Summary</title>
<description>Weather Warning Summary provided by the Hong Kong Observatory</description>
<link>http://www.weather.gov.hk/textonly/warning/warn.htm</link>
<language>en-us</language>
<webMaster>mailbox@hko.gov.hk</webMaster>
<copyright>The content available in this file, including but not limited to all text, graphics, drawings, diagrams, photographs and compilation of data or other materials are protected by copyright. The Government of the Hong Kong Special Administrative Region is the owner of all copyright works contained in this website.</copyright>
<pubDate>Tue, 07 Aug 2018 05:31:48 +0800</pubDate>
<image>
<title>Hong Kong Observatory</title>
<url>http://rss.weather.gov.hk/img/logo_dblue.gif</url>
<width>333</width>
<height>65</height>
</image>
<item>
<title><![CDATA[Thunderstorm Warning issued (04:24 HKT 07/08/2018) ]]></title>
<link>http://www.weather.gov.hk/textonly/warning/warn.htm</link>
<author>Hong Kong Observatory</author>
<description><![CDATA[Thunderstorm Warning was issued at 04:24 HKT (7 Aug 2018) <br/><br/>]]></description>
<pubDate>Tue, 07 Aug 2018 04:24:00 +0800</pubDate>
<guid isPermaLink="false">http://rss.weather.gov.hk/rss/201808070424/Thunderstorm Warning issued</guid>
</item>
<item>
<title><![CDATA[Very Hot Weather Warning issued (08:30 HKT 03/08/2018) ]]></title>
<link>http://www.weather.gov.hk/textonly/warning/warn.htm</link>
<author>Hong Kong Observatory</author>
<description><![CDATA[Very Hot Weather Warning was issued at 08:30 HKT (3 Aug 2018) <br/><br/>]]></description>
<pubDate>Fri, 03 Aug 2018 08:30:00 +0800</pubDate>
<guid isPermaLink="false">http://rss.weather.gov.hk/rss/201808030830/Very Hot Weather Warning issued</guid>
</item>
</channel>
</rss>
我希望输出是每个Item元素的标题。
非常感谢您的帮助。