我有一个奇怪的问题。奇怪的是,代码在游乐场中执行得非常完美,但在模拟器中却没有...
长话短说,我有一个类应该解析我懒惰实例化的文档并从VC调用解析方法。解析函数在类中调用,但不在委托函数中调用(解析开始,找到字符等)。这有点令人抓狂,因为同样的设置在操场上运行得很好......
解析类(和协议):
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 DexVC: UIViewController, DefintionFetching {
lazy var definitionFetcher: DefinitionFetcher = DefinitionFetcher(delegate: self)
func fetchingDidFinish(results: [String: NSMutableAttributedString]) { }
@IBAction func doShit(sender: AnyObject) {
definitionFetcher.fetch()
}
}
任何帮助都将受到极大的赞赏!
答案 0 :(得分:4)
您替换了初始实例中的parser
属性。
func fetch(){
println("fetch called")
parser = NSXMLParser(contentsOfURL: url)!
// ^^^^^^^^^ HERE
parser.parse()
}
所以,parser.delegate = self
中的init
完全没有意义。
尝试:
func fetch(){
println("fetch called")
parser = NSXMLParser(contentsOfURL: url)!
parser.delegate = self // <-- ADDED
parser.parse()
}