Swift NSXMLParserDelegate 没有被调用
Swift NSXMLParserDelegate doesn't get called
我有一个奇怪的问题。奇怪的是,代码在操场上执行得非常完美,但在模拟器中却没有……
长话短说,我有一个 class 应该解析一个文档,我懒惰地实例化并从 VC 调用解析方法。在 class 中调用解析函数,但不调用委托函数(解析开始,找到字符等)。这有点令人抓狂,因为同样的设置在操场上工作得很好......
正在解析 class(和协议):
protocol DefintionFetching {
func fetchingDidFinish(results: [String: NSMutableAttributedString])
}
class DefinitionFetcher: NSObject, NSXMLParserDelegate {
var delegate: DefintionFetching
var parser = NSXMLParser()
let url = NSURL(string: "http://dexonline.ro/definitie/soapta/xml")!
var element = String()
var finalDefinitions = [String: NSMutableAttributedString]()
init(delegate: DefintionFetching) {
self.delegate = delegate
super.init()
parser.delegate = self
}
func fetch() {
println("fetch called") // this gets called
parser = NSXMLParser(contentsOfURL: url)!
parser.parse()
}
func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject: AnyObject]!) {
// this doesn't get called
}
}
并且 VC 调用 class:
class DexVC: UIViewController, DefintionFetching {
lazy var definitionFetcher: DefinitionFetcher = DefinitionFetcher(delegate: self)
func fetchingDidFinish(results: [String: NSMutableAttributedString]) { }
@IBAction func doShit(sender: AnyObject) {
definitionFetcher.fetch()
}
}
任何帮助将不胜感激!
您从初始实例中替换了 parser
属性。
func fetch(){
println("fetch called")
parser = NSXMLParser(contentsOfURL: url)!
// ^^^^^^^^^ HERE
parser.parse()
}
所以,init
中的parser.delegate = self
完全没有意义。
尝试:
func fetch(){
println("fetch called")
parser = NSXMLParser(contentsOfURL: url)!
parser.delegate = self // <-- ADDED
parser.parse()
}
我有一个奇怪的问题。奇怪的是,代码在操场上执行得非常完美,但在模拟器中却没有……
长话短说,我有一个 class 应该解析一个文档,我懒惰地实例化并从 VC 调用解析方法。在 class 中调用解析函数,但不调用委托函数(解析开始,找到字符等)。这有点令人抓狂,因为同样的设置在操场上工作得很好......
正在解析 class(和协议):
protocol DefintionFetching {
func fetchingDidFinish(results: [String: NSMutableAttributedString])
}
class DefinitionFetcher: NSObject, NSXMLParserDelegate {
var delegate: DefintionFetching
var parser = NSXMLParser()
let url = NSURL(string: "http://dexonline.ro/definitie/soapta/xml")!
var element = String()
var finalDefinitions = [String: NSMutableAttributedString]()
init(delegate: DefintionFetching) {
self.delegate = delegate
super.init()
parser.delegate = self
}
func fetch() {
println("fetch called") // this gets called
parser = NSXMLParser(contentsOfURL: url)!
parser.parse()
}
func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject: AnyObject]!) {
// this doesn't get called
}
}
并且 VC 调用 class:
class DexVC: UIViewController, DefintionFetching {
lazy var definitionFetcher: DefinitionFetcher = DefinitionFetcher(delegate: self)
func fetchingDidFinish(results: [String: NSMutableAttributedString]) { }
@IBAction func doShit(sender: AnyObject) {
definitionFetcher.fetch()
}
}
任何帮助将不胜感激!
您从初始实例中替换了 parser
属性。
func fetch(){
println("fetch called")
parser = NSXMLParser(contentsOfURL: url)!
// ^^^^^^^^^ HERE
parser.parse()
}
所以,init
中的parser.delegate = self
完全没有意义。
尝试:
func fetch(){
println("fetch called")
parser = NSXMLParser(contentsOfURL: url)!
parser.delegate = self // <-- ADDED
parser.parse()
}