ios - how to connect RSS feed with swift -
i want make application news. decided use rss feed somehow rss feed doesn't come up.
here code how declare variable
var parser = nsxmlparser() var feeds = nsmutablearray() var elements = nsmutabledictionary() var element = nsstring() var ftitle = nsmutablestring() var link = nsmutablestring() var fdescription = nsmutablestring()
this in viewdidload function
feeds = [] var url: nsurl = nsurl(string:"https://www.apple.com/main/rss/hotnews/hotnews.rss")! parser = nsxmlparser(contentsofurl: url)! parser.delegate = self parser.shouldprocessnamespaces = false parser.shouldreportnamespaceprefixes = false parser.shouldresolveexternalentities = false parser.parse()
this didstartelement function.
func parser(parser: nsxmlparser!, didstartelement elementname: string!, namespaceuri: string!, qualifiedname qname: string!, attributes attributedict: [nsobject : anyobject]!) { element = elementname // instantiate feed properties if(element nsstring).isequaltostring("item"){ elements = nsmutabledictionary.alloc() elements = [:] ftitle = nsmutablestring.alloc() ftitle = "" link = nsmutablestring.alloc() link = "" fdescription = nsmutablestring.alloc() fdescription = "" }
this didendelement
func parser(parser: nsxmlparser!, didendelement elementname: string!, namespaceuri: string!, qualifiedname qname: string!) { //process feed elements if(elementname nsstring).isequaltostring("item") { if ftitle != "" { elements.setobject(ftitle, forkey: "title") } if link != ""{ elements.setobject(link, forkey: "link") } if fdescription != "" { elements.setobject(fdescription, forkey: "decription") } feeds.addobject(elements) } }
this foundcharacter function
func parser(parser: nsxmlparser!, foundcharacters string: string!) { if element.isequaltostring("title") { ftitle.appendstring(string) } else if element.isequaltostring("link") { link.appendstring(string) } else if element.isequaltostring("description") { fdescription.appendstring(string) } }
this cellforrowatindexpath function
let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) uitableviewcell // configure cell... cell.textlabel?.text = (feeds.objectatindex(indexpath.row).objectforkey("tittle") string) cell.detailtextlabel?.numberoflines = 3 cell.detailtextlabel?.text = (feeds.objectatindex(indexpath.row).objectforkey("description") string) return cell
am missing something? suggestion?
Comments
Post a Comment