gpt4 book ai didi

ios - NSXML 使用 Swift 将损坏的字符串解析为数组

转载 作者:搜寻专家 更新时间:2023-11-01 06:21:16 24 4
gpt4 key购买 nike

使用 NSXMLParser,在下面的代码中 conditionIconString = conditionIconString + string 将链接存储到 1 个大字符串中,但我需要它将每个链接作为字符串存储在 [String]。我不知道该怎么做?

var conditionsIcons = [String]()
var conditionIconString = String()
var weAreInsideAnItem = false

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {


if (elementName as NSString).isEqualToString("icon-link") {
weAreInsideAnItem = true

}


if (elementName as NSString).isEqualToString("conditions-icon") {
conditionsIcons = [String]()
conditionsIcons = [""]
}

func parser(parser: NSXMLParser, foundCharacters string: String) {


if weAreInsideAnItem == true {
if element.isEqualToString("icon-link") {
conditionIconString = conditionIconString + string
}
}
}

func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if (elementName as NSString).isEqualToString("icon-link") {
conditionsIcons.append(conditionIconString)
weAreInsideAnItem = false
}
elements.setObject(conditionsIcons, forKey: "conditionIcons")
}

这是我正在解析的 XML:http://forecast.weather.gov/MapClick.php?lat=39.557&lon=-79.3542&unit=0&lg=english&FcstType=dwml

<conditions-icon type="forecast-NWS" time-layout="k-p12h-n13-1">
<name>Conditions Icon</name>
<icon-link>
http://forecast.weather.gov/newimages/medium/wind_sct.png
</icon-link>
<icon-link>
http://forecast.weather.gov/DualImage.php?i=nra_sn&j=nsn&ip=20&jp=40
</icon-link>
<icon-link>
http://forecast.weather.gov/DualImage.php?i=sn&j=bkn&ip=20
</icon-link>
<icon-link>
http://forecast.weather.gov/newimages/medium/nsct.png
</icon-link>
<icon-link>
http://forecast.weather.gov/newimages/medium/sct.png
</icon-link>
<icon-link>
http://forecast.weather.gov/newimages/medium/nsct.png
</icon-link>
<icon-link>
http://forecast.weather.gov/newimages/medium/sct.png
</icon-link>
<icon-link>
http://forecast.weather.gov/newimages/medium/nsct.png
</icon-link>
<icon-link>
http://forecast.weather.gov/newimages/medium/bkn.png
</icon-link>
<icon-link>
http://forecast.weather.gov/newimages/medium/nbkn.png
</icon-link>
<icon-link>
http://forecast.weather.gov/newimages/medium/bkn.png
</icon-link>
<icon-link>
http://forecast.weather.gov/newimages/medium/nshra40.png
</icon-link>
<icon-link>
http://forecast.weather.gov/newimages/medium/bkn.png
</icon-link>
</conditions-icon>

最佳答案

问题是 conditionIconString永远不会重置,它随着每个 <icon-link> 的增长而增长找到的标签。更新 didEndElement在开始解析下一个元素之前将此字符串设置回空的函数:

func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if (elementName as NSString).isEqualToString("icon-link") {
conditionsIcons.append(conditionIconString)

conditionIconString = String() // THIS IS THE KEY LINE

weAreInsideAnItem = false
}
elements.setObject(conditionsIcons, forKey: "conditionIcons")
}

关于ios - NSXML 使用 Swift 将损坏的字符串解析为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33698576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com