简体   繁体   English

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

[英]How can I combine Alamofire with AEXML or SWXMLHASH

I want to parse a XML from URL which is RSS of my blog with SWXMLHASH or AEXML libraries. 我想从URL中解析XML,这是我的博客的RSS和SWXMLHASH或AEXML库。 I found a solution for this but when I tried the solution my app crashed. 我找到了解决方案但是当我尝试解决方案时,我的应用程序崩溃了。 It is the encountered problem: fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) 这是遇到的问题: fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

And there are my codes which are in viewDidLoad: 我的代码在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!)
        }
        }
`

Is there any other solution for this? 还有其他解决方案吗? I don't want to use NSXMLParser, I want to use these kind of libraries or If it is exist any up-to-date library, I can use. 我不想使用NSXMLParser,我想使用这些类或者如果它存在任何最新的库,我可以使用。

Thank you for your help! 谢谢您的帮助!

Use 采用

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

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

I used AEXML.. try this 我使用了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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM