正在将 RSS 提要加载到 IOS Swift
Loading RSS Feed to IOS Swift
我正在尝试将 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
我做错了什么?如果是这样参考或如何解决它?会非常有帮助!
你需要使用if let
来避免异常:
if let allItems = channel.items {
self.items = allItems
self.videoTableView.reloadData()//Exception on this line
}
I am also able to run your project, please check below simulator
image:
我正在尝试将 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
我做错了什么?如果是这样参考或如何解决它?会非常有帮助!
你需要使用if let
来避免异常:
if let allItems = channel.items {
self.items = allItems
self.videoTableView.reloadData()//Exception on this line
}
I am also able to run your project, please check below simulator image: