parser.parse() in Swift leads to EXC_BAD_ACCESS
Asked Answered
V

3

8

I'm following this tutorial as a jump start for an RSS feeder app I'm working on in Swift. I know there are some things that have changed in Swift since this tutorial, but none of them seem to explain why I'm having this issue.

Relevant Code (as far as I can tell) is as follows in my TableViewController:

 override func viewDidLoad() {
    super.viewDidLoad()

    let url:NSURL = NSURL(string: "my.url.string")
    parser = NSXMLParser(contentsOfURL: url)
    parser.delegate = self
    parser.parse() // <- Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

}

There doesn't seem to be a problem with the actual parser delegate methods as I put breakpoints on them and they aren't even being called before the crash.

My assumption is that it's a Swift bug, but I wanted to make sure I wasn't missing something before I go complaining to apple about it.

Varicella answered 11/9, 2014 at 17:47 Comment(1)
I'm glad David was able to answer my question, even though I didn't include the the actual ParserDelegate methods. To be clear; my original methods looked something like this: func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String, qualifiedName qName: String, attributes attributeDict: [NSObject : AnyObject]) and now they look something like this: func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject : AnyObject]!) Thanks again David!Varicella
I
13

There seems to be an error in the automatically translated headers that assumes that qualified name spaces are always used, however, since they can be nil sometimes, it crashes.

If you use:

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

by making the namespace and qualifiedName parameters implicitly unwrapped (or explicitly wrapped should work as well) you should be good to go.

You'll probably have to make similar changes for any delegate methods you provide that take namespaceURI or qualifiedName parameters.

Insurgence answered 11/9, 2014 at 22:28 Comment(4)
That did it! Thanks a lot. The wrapped and unwrapping optional stuff is far and away the thing I'm having the hardest time understanding in swift. Do you know of a blog post or tutorial somewhere that could help me nail that down?Varicella
+1 This completely saved me. I was seeing an EXC_BAD_ACCESS when parsing a perfectly good XML string - I updated both my didStartElement and didEndElement delegate methods to implicitly unwrap the namespaceURI, qName, and attributeDict arguments, and it's now working great. Thank you!Merritt
I don't think attributeDict needs to be optional. Pretty sure that in my experiments it was always at least an empty dictionary.Insurgence
Brilliant, saved my evening! I have yet to understand why it works exactly, but many thanks!Foreknow
L
0

This bug is fixed in XCode Version 6.1 (6A1052c), in the same way @David has already suggested, but just for the record, it's fixed now.

Lee answered 4/11, 2014 at 11:31 Comment(0)
L
0

if you face with EXC_BAD_ACCESS now in 2021, check if you call parser in main UI thread. Don't parse in network callback. THis was my case.

Lamellate answered 11/5, 2021 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.