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

时间:2017-05-24 21:08:15

标签: ios swift xml-parsing alamofire

我想从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,我想使用这些类或者如果它存在任何最新的库,我可以使用。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

使用

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)
        }
}