当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Swift 3 XML Parse Delegates not Called after data sent from Watch to Phone

问题描述:

I'm trying to understand how delegates work for a project. The idea is to pick a section from a table view on the watch app and it parse the xml on the Phone. But when I click a section to start the Parser nothing happens.

So when the phone loads up I do the following.

override func viewDidLoad() {

super.viewDidLoad()

parser.delegate = self

if (WCSession.isSupported()) {

print("Phone Connected")

session = WCSession.default()

session.delegate = self

session.activate()

}

}

When I click on a section to send the message to the phone from the watch I make try to begin parsing the data for the url.

func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {

parseSection(s: message["section"]! as! String)

}

func parseSection(s: String){

let link = "http://www.espn.com/espn/rss/" + s

let url : URL = URL(string: link)!

parser = XMLParser(contentsOf: url)!

parser.parse()

}

But the following delegates do nothing.

func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {

print("Parser Begins")

}

func parser(_ parser: XMLParser, foundCharacters string: String) {

print("Characters Found")

}

func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {

print("Parser Ends")

}

网友答案:

Your class needs to implement the XMLParserDelegate protocol in order to get the parsing functions called. You already did that but you missed to set the delegate for the XMLParser:

func parseSection(s: String){
    let link = "http://www.espn.com/espn/rss/" + s 
    let url : URL = URL(string: link)!
    parser = XMLParser(contentsOf: url)!
    parser.delegate = self  // <-- set the delegate
    parser.parse()
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: