Swift - 不稳定的XML解析器

时间:2016-06-25 19:31:17

标签: swift debugging nsxmlparserdelegate

我在swift中构建XML解析器,我遇到了几个问题。第一:Xcode有时会抛出新的错误,例如BAD_ACCESS,有时会完美运行。第二种:同样的情况,我有时会在将pubDate转换为NSDate的字符串时遇到错误,有时它的工作绝对完美。问题是我使用for循环解析来自多个链接的数据,并且在该循环中我使用Utility类进行调度。我想这可能是问题所在。

以下是我NSXMLParserDelegate的代码:

class Parser: NSObject, NSXMLParserDelegate {

    var title: String!
    var link: String!
    var pubDate: String!
    var currentElement = String()
    var isInItem = false
    var URL: String!
    var lastBuildDate: String!
    var parser: NSXMLParser!
    var delegate: ParserDelegate!
    var currentSource = [String]()
    var all = [Item]()

    var count = 0

    init(currentSource: [String]) {
        self.currentSource = currentSource
    }

    func parse(){
        all.removeAll()
        count = 0
        for url in currentSource {
            dispatch(queue: .Background) {
                self.parser = NSXMLParser(contentsOfURL: NSURL(string: url)!)
                self.parser.delegate = self
                self.parser.parse()
            }
        }
    }

    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
        if elementName == "item"{
            isInItem = true
            link = ""
            title = ""
            pubDate = ""
        }
        else if elementName == "lastBuildDate"{
            lastBuildDate = String()
        }

        if isInItem{
            switch elementName{
            case "pubDate":
                pubDate = String()
                currentElement = "pubDate"
            case "title":
                title = String()
                currentElement = "title"
            case "link":
                link = String()
                currentElement = "link"
            default:
                break
            }
        }
    }

    func parser(parser: NSXMLParser, foundCharacters string: String) {
        if isInItem{
            switch currentElement {
            case "pubDate":
                pubDate = pubDate + string
            case "title":
                title = title + string
            case "link":
                link = link + string
            default:
                break
            }
        }
        else if currentElement == "lastBuildDate"{
            lastBuildDate = lastBuildDate + string
        }
    }

    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
        if isInItem{
            currentElement = ""
        }

        if elementName == "item" {

            isInItem = false
            let item = Item()
            item.link = link
            item.title = title
            item.timeAgo = Double.time(NSDate().timeIntervalSinceDate(NSDate.getDate(pubDate)))
            item.timeDiff = NSDate().timeIntervalSinceDate(NSDate.getDate(pubDate))
            all.append(item)
        }
    }

    func parserDidEndDocument(parser: NSXMLParser) {
        count += 1
        if count == currentSource.count{
            all.sortInPlace({$0.timeDiff < $1.timeDiff})
            delegate.parseArray(all)
            print(all.count)
        }
    }
}

0 个答案:

没有答案