gpt4 book ai didi

ios - 如何将 Alamofire 与 AEXML 或 SWXMLHASH 结合使用

转载 作者:行者123 更新时间:2023-11-28 08:09:10 25 4
gpt4 key购买 nike

我想使用 SWXMLHASH 或 AEXML 库从我博客的 RSS URL 解析 XML。我为此找到了解决方案,但是当我尝试该解决方案时,我的应用程序崩溃了。是遇到的问题:fatal error: unwrapping an Optional value unwhile unwrapping an Optional value unexpectedly found nil
(lldb)

我的代码在 viewDidLoad 中:

` Alamofire.request("http://myblog.com/feed").responseJSON { response in

if let data = response.data {

let xml = SWXMLHash.parse(data)
let news = xml["rss"]["channel"]["item"][0]["title"].element?.text
print(news!)
}
}
`

还有其他解决办法吗?我不想使用 NSXMLParser,我想使用这些类型的库,或者如果它存在任何最新的库,我可以使用。

感谢您的帮助!

最佳答案

使用

Alamofire.request("http://myblog.com/feed").response

不是 Alamofire.request("http://myblog.com/feed").responseJSON

我使用了 AEXML..试试这个

Alamofire.request("http://myblog.com/feed").response { res in

guard let xml = res.data else {
return
}

var options = AEXMLOptions()
options.parserSettings.shouldProcessNamespaces = false
options.parserSettings.shouldReportNamespacePrefixes = false
options.parserSettings.shouldResolveExternalEntities = false

do {
let xmlDoc = try AEXMLDocument(xml: xml, options: options)

print(xmlDoc.root["channel"]["item"]["title"].value!)

} catch let error {
print(error)
}
}

关于ios - 如何将 Alamofire 与 AEXML 或 SWXMLHASH 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44168495/

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