我对Swift很新,而不是常规的编码器,所以道歉,我很遗憾没有明显的东西!
我有一个NSXMLParser类在尝试将结果项添加到数组时给我一个EXC_BAD_ACCESS错误。
我相信(使用Xcode调试器)我有一个有效的安全(我的结果类)实例。
我的结果数组是空的,当我收到错误时,我正试图追加第一项。
课程如下:
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)
}
}
}
我的新安全值是:
newSecurity XMLTest.Security 0x00007f863b7368d0 0x00007f863b7368d0
securityName字符串“”
securityTicker字符串“HSX.L”
purchasePrice Float 0
latestPrice Float 861
stopLossPrice Float 0
profitPercent Float 0
更改Float 29
如果没有明显错误,我还应该寻找其他什么来找到错误?
感谢。