将RSS Feed加载到IOS Swift

时间:2018-09-03 09:18:48

标签: ios swift uitableview exception rss-reader

我正在尝试将RSS Feed加载到IOS应用程序。经过大量的教程后发现以下内容,但它引发了异常。

来源:https://github.com/tichise/TIFeedParser

 func loadRSS() {

        let feedUrlString:String = "https://news.google.com/news?hl=us&ned=us&ie=UTF-8&oe=UTF-8&output=rss"

        Alamofire.request(feedUrlString).response { response in

            if let data = response.data, let _ = String(data: data, encoding: .utf8) {

                TIFeedParser.parseRSS(xmlData: data as NSData, completionHandler: {(isSuccess, channel, error) -> Void in

                    if (isSuccess) {
                        self.items = channel!.items!
                        self.videoTableView.reloadData()//Exception on this line
                    }

                    if (response.error != nil) {
                        print((response.error?.localizedDescription)! as String)
                    }
                })
            }
        }

    }

异常是线程1:致命错误:展开一个可选值时意外发现nil

我做错了什么?如果是这样,请参考或如何解决?将会非常有帮助!

1 个答案:

答案 0 :(得分:1)

您需要使用if let来避免出现异常:

if let allItems = channel.items {
       self.items = allItems
       self.videoTableView.reloadData()//Exception on this line
}
  

我也可以运行您的项目,请在下面的模拟器中检查   图片:

enter image description here