为什么在 swift 4 中解析 xml 文件会 return 不完整?

why parsing xml file in swift 4 will return incomplete?

所以这是我用来解析 xml 的代码,我在打印和 table 视图中收到的文本不完整

func parser(_ parser: XMLParser, foundCharacters string: String) {
    let data = string.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)

    print(data)
    if data.count != 0 {
        switch termsItem {

        case "title" : termsTitle = data
        case "body" : termsTexts = data
            default :
            break


        }


    }
}

我将此代码更改为下面的代码,现在 xml 的打印是正确的但是我在 table 视图

中看不到任何内容
func parser(_ parser: XMLParser, foundCharacters string: String) {
    let data = string

    print(data)
    if data.count != 0 {
        switch termsItem {

        case "title" : termsTitle = data
        case "body" : termsTexts = data
            default :
            break


        }


    }
}

这是其他代码,但我没有更改它们

func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {


    termsItem = elementName

}

func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {


    if elementName == "item" {

        var theItems = Item()
        theItems.title = termsTitle
        theItems.body = termsTexts
        print(theItems)
        print(theItems.title)
        print(termsTitle)
        tableViewterms.append(theItems)

    }

    DispatchQueue.main.async {

        self.otherTopicsTableView.reloadData()



    }

  }
}

所以我不得不附加数据然后删除它们现在这段代码运行良好

 func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {

    termsItem = elementName

}

func parser(_ parser: XMLParser, foundCharacters string: String) {
    let data = string.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)

    if data.count != 0 {
        switch termsItem {

        case "title" : termsTitle = data
        case "body" : termsTexts.append(contentsOf: data)
            default :
            break

        }
    }
}


func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {


    if elementName == "item" {

        var theItems = Item()
        theItems.title = termsTitle
        theItems.body = termsTexts
        print(theItems.body)
        termsTexts.removeAll()
        tableViewterms.append(theItems)

    }

    DispatchQueue.main.async {

        self.otherTopicsTableView.reloadData()

    }

}