3

I'm newish to Swift, and not a regular coder anymore so apologies, chance are I'm missing something obvious!

I have an NSXMLParser class that is giving me a EXC_BAD_ACCESS fault when attempting to add a result item to an array.

I believe (using Xcode debugger) that I have a valid Security (my result class) instance.

My result array is empty and I'm attempting to append the first item when I get the error.

The class is below:

class YahooXMLParser: NSXMLParser, NSXMLParserDelegate{

    var results: [Security] = []
    var parser: NSXMLParser = NSXMLParser()

    var xSecurityTicker: String = String()
    var xPurchasePrice: String = String()
    var xLatestPrice: String = String()
    var xStopLossPrice: String = String()
    var xChange: String = String()
    var xProfitPercent: String = String()
    var eName: String = String()

    func parseYahoo(url: NSURL) ->[Security]{
        parser = NSXMLParser(contentsOfURL: url)!
        parser.delegate = self
        parser.parse()
        return results
    }

    // NSXMLPArser Delegate functions

    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {
        eName = elementName
        if elementName == "col0" {
            xSecurityTicker = String()
        }
        if elementName == "col1" {
            xLatestPrice = String()
        }
        if elementName == "col4" {
            xChange = String()
        }
    }

    func parser(parser: NSXMLParser, foundCharacters string: String?) {
        let data = string!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
        if (!data.isEmpty) {
            if eName == "col0" {
                xSecurityTicker += data
            } else if eName == "col1" {
                xLatestPrice += data
            } else if eName == "col4" {
                xChange += data
            }
        }
    }

    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
        if elementName == "row" {
            let newSecurity = Security(ticker: xSecurityTicker,
                latest: NSString(string: xLatestPrice).floatValue,
                change: NSString(string: xChange).floatValue)

            results.append(newSecurity)
        }

    }

}

My newSecurity values are:


newSecurity XMLTest.Security 0x00007f863b7368d0 0x00007f863b7368d0

securityName String ""

securityTicker String "HSX.L"

purchasePrice Float 0

latestPrice Float 861

stopLossPrice Float 0

profitPercent Float 0

change Float 29


If there isn't an obvious error what else should I be looking for to find the fault?

Thanks.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Ian J
  • 31
  • 1
  • In which line do you get the error? – qwerty_so May 27 '15 at 19:39
  • results.append(newSecurity) – Ian J May 27 '15 at 19:42
  • Smells like one of those awful compiler bugs. Try a `println` right before the `append` and see what `result` contains. – qwerty_so May 27 '15 at 21:08
  • Thanks - The println helped. Still not sure why but results wasn't properly accessible. Despite Xcode debugger showing it as "([XMLTest.Security]) 0 items" - which I thought meant it was, correctly an empty array of Security items. Adding an initialiser for 'results = []' at the start of parseYahoo() solved it. The initialiser at the start of the class should have done the same thing shouldn't it? – Ian J May 28 '15 at 16:15
  • The debugger itself needs debugging. I often found it does not display the correct values. Obj-C did behave much better here. – qwerty_so May 30 '15 at 11:08

0 Answers0