将RSS Feed加载到IOS Swift

问题描述 投票:0回答:1

我正在尝试将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:致命错误:在展开Optional值时意外发现nil

我究竟做错了什么 ?如果是这样参考或如何解决它?会非常有帮助的!

ios swift uitableview exception rss-reader
1个回答
1
投票

你需要使用if let来避免异常:

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

我也可以运行你的项目,请检查下面的模拟器图像:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.