NSXMLParser: EXC_BAD_ACCESS error
I'm a newbie to Swift, not a regular coder, so I'm sorry, chances are I'm missing something obvious!
I have an NSXMLParser class that gives me an EXC_BAD_ACCESS error when I try to add a result item to an array.
I believe (using the Xcode debugger) that I have a valid instance of Security (my result class).
My result array is empty and I am trying to add the first item when I get an error.
Class 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:
newSecurity XMLTest.Security 0x00007f863b7368d0 0x00007f863b7368d0
securityName String ""
securityTicker String "HSX.L"
purchasePrice Float 0
lastPrice Float 861
stopLossPrice Float 0
profitPercent Float 0
change Float 29
If there is no obvious error, what else should I be looking for to find the error?
Thank.
source to share
No one has answered this question yet
Check out similar questions: