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

标签 ios swift nsxmlparser

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

这是我的tableView.swift 类

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,但它仍然没有被调用。

关于我可以做些什么来让它工作有什么想法吗?这是我到目前为止的所有代码。也许我需要导入某个框架?

最佳答案

尝试:

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

更正:实际上,它不是最后一个参数,它是 namespaceURInameSpaceURI 在你原来的拼写。

从工作 field 再次复制并粘贴。

关于ios - 永远不会调用 Xml 解析器函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24716435/

相关文章:

iphone - NSXMLParser 如何将 NSMutableDictionary 传递给 NSMutableArray

iphone - 应用程序打开时,系统是否可以删除 NSCachesDirectory 的内容?

objective-c - 为什么 View 在自定义缩放和翻转动画后绘制在任务栏下方?

swift - 如何使用 Swift 检查 float 的 float ?

ios - 将自定义 SKShader 应用于 SKScene,使用 Swift 在 iOS 8 SpriteKit 中对整个渲染场景进行像素化

ios - 使用 NSXMLParser 解析复杂的 XML 结构

ios - 如何在 iPhone 编程中解析 SOAP xml 响应

ios - 使用 'YoutubePlayer Swift SDK' 在 tableviewCell 中播放 youtube 视频

javascript - 带小数点分隔符的 HTML 数字输入问题

iOS 加载自定义注解