Vote count:
0
Hi i'm trying to work on a simple exercise. i made this basic xml rss reader, it works fine but i would love to improve it and worry about async loading. How should i do?
import UIKit
class ViewController: UITableViewController, NSXMLParserDelegate, UITableViewDelegate {
let urlstring = "http://ift.tt/1merQXG"
var element:NSString = ""
var items:[String] = []
var item = ""
override func viewDidLoad() {
super.viewDidLoad()
// dispatch_queue_t myQueue = dispatch_queue_create("queue",NULL)
loadParser()
}
//MARK - tableviewdelegate
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel.text = items[indexPath.row]
return cell
}
//MARK - parser
func loadParser(){
let url = NSURL(string: urlstring)
var parser = NSXMLParser(contentsOfURL: url)
parser?.delegate = self
parser?.shouldProcessNamespaces = true
parser?.shouldReportNamespacePrefixes = true
parser?.shouldResolveExternalEntities = true
parser?.parse()
}
//MARK: - Parser Delegate
func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject : AnyObject]!) {
element = elementName
if ((elementName as NSString).isEqualToString("item")){
item = ""
}
}
func parser(parser: NSXMLParser!, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!) {
if ((elementName as NSString).isEqualToString("item")){
items.append(item)
}
}
func parser(parser: NSXMLParser!, foundCharacters string: String!) {
if ( (element.isEqualToString("title")) && (element != "") ){
item += string
}
}
func parserDidEndDocument(parser: NSXMLParser!) {
println(items)
self.tableView.reloadData()
}
}
i am not sure how to do. i'm trying to wrap the "loadParser" in a dispatch queue but in swift it's not working. can you guys tell me how to do that using GCD ? thanks
asked 57 secs ago
swift xml rss reader... make it async
Aucun commentaire:
Enregistrer un commentaire