永远不会调用Xml解析器函数

时间:2014-07-12 19:15:06

标签: ios swift nsxmlparser

我目前正在使用swift并尝试制作和RSS阅读器iOS iPhone应用程序。我目前在调用解析器函数时遇到了麻烦。我真的想用Swift复制我在Objective-C中开发的内容(效果很好)。

这是我的tableView.swift class

import UIKit

class FeedTableViewController: UITableViewController, NSXMLParserDelegate {

    var parser: NSXMLParser = NSXMLParser()
    var feeds: NSMutableArray = []
    var fItem = Dictionary<String, Float>()
    var fTitle: String = String()
    var element: String = String()


    override func viewDidLoad() {
        super.viewDidLoad()

        var url: NSURL = NSURL.URLWithString("http://feeds.feedburner.com/TouchCodeMagazine")
        parser = NSXMLParser(contentsOfURL: url)
        parser.delegate = self
        parser.shouldResolveExternalEntities = false
        parser.parse()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // #pragma mark - Table view data source
    override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
        return feeds.count
    }

    override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {

        let CellId: NSString = "Cell"

        var cell: UITableViewCell = tableView?.dequeueReusableCellWithIdentifier(CellId) as UITableViewCell

        if let ip = indexPath {
            cell.textLabel.text = "hello\(ip.row)"
        }
        return cell
    }

   // This function is never called.
   func parser(parser: NSXMLParser!, didStartElement elementName: String!, nameSpaceURI namespaceURI: String!, qualifiedName: String!, attributes attributeDict: Dictionary<String, Float>) {

        element = elementName
        println(element) // This line is never executed.
    }
}

我已经设置了NSXMLParserDelegate,但它永远不会被调用。

知道我能做些什么才能让这个工作?这是我到目前为止的所有代码。也许我需要导入某个框架?

1 个答案:

答案 0 :(得分:3)

尝试:

func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName: String!, attributes: Dictionary<String, Float>) {
    println(elementName) // This line is never executed.
}

更正:实际上,它不是最后一个参数,而是原始版本中namespaceURInameSpaceURI的拼写。

再次从工作场所复制并粘贴。

相关问题