Search code examples
iosswiftuitableviewexceptionrss-reader

Loading RSS Feed to IOS Swift


I am trying to load RSS Feed to an IOS Application. Went through quite the number of tutorials found the below but it throws an exception.

Source : 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)
                    }
                })
            }
        }

    }

Exception is Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

What am I doing wrong ? If so reference or How do to fix it ? would be immensely helpful !


Solution

  • You need to use if let to avoid exception:

    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:

    enter image description here