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.