我目前正在使用 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.
}
更正:实际上,它不是最后一个参数,它是 namespaceURI
与 nameSpaceURI
在你原来的拼写。
从工作 field 再次复制并粘贴。
关于ios - 永远不会调用 Xml 解析器函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24716435/