[英]How can I combine Alamofire with AEXML or SWXMLHASH
我想從URL中解析XML,這是我的博客的RSS和SWXMLHASH或AEXML庫。 我找到了解決方案但是當我嘗試解決方案時,我的應用程序崩潰了。 這是遇到的問題: fatal error: unexpectedly found nil while unwrapping an Optional value (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)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.