Search code examples
iosswiftnsxmlparser

NSXMLParser crashes when 'didEndElement' method is implemented


I have a problem with using the NSMXLParser.

first of all here is my code:

class ViewController: UIViewController, NSXMLParserDelegate {


    override func viewDidLoad() {

        super.viewDidLoad()

        var url = NSURL(string: "http://placeholder.abc/data.xml")
        var xmlParser = NSXMLParser(contentsOfURL: url)
        xmlParser.delegate = self
        xmlParser.parse()
    }

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String, qualifiedName qName: String, attributes attributeDict: [NSObject : AnyObject]) {

     println(elementName)
}

func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String, qualifiedName qName: String) {

}

func parser(parser: NSXMLParser, foundCharacters string: String) {

}

Now the problem is: As soon as I implement the didEndElement method my application crashes while executing this code line: xmlParser.parse() with this Error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

When I delete the didEndElement method, the code runs fine. Can someone help me? Thanks!


Solution

  • I'm developing an iOS app since several months so it made its way through all the beta versions of Xcode. The XMLParser part in that app has this signature:

    func parser(parser: NSXMLParser!, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!) {
        ...
    }
    

    This works without errors.

    When I remove the exclamation marks, the app crashes with EXC_BAD_ACCESS

    All other methods of NSXMLParserDelegate have the same signature: All variables are bound as implicitly unwrapped optionals (all with !)