代码之家  ›  专栏  ›  技术社区  ›  BPDESILVA

正在将RSS源加载到IOS Swift

  •  0
  • BPDESILVA  · 技术社区  · 6 年前

    我正在尝试将RSS提要加载到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 回复  |  直到 6 年前
        1
  •  1
  •   Jogendar Choudhary    6 年前

    你需要使用 if let 为避免例外:

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

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

    enter image description here