【问题标题】:Value of type 'XPathObject' has no member 'text'“XPathObject”类型的值没有成员“文本”
【发布时间】:2019-01-05 00:41:56
【问题描述】:

我正在尝试在不使用循环的情况下复制 for 循环,该循环有效且已被注释掉。

如何在不使用for 循环的情况下访问文本成员?

if let doc = try? Kanna.HTML(html: htmlText, encoding: .utf8)
{
    for item in doc.css("div[class^='test']")
    {
        print("item: \(count)")                                           
        let itemName = item.css("p[class^='hello']")
        /*
        for itemName in item.css(("p[class^='hello']"))
        {
            print(itemName.text!)
        }
        */
        print(itemName.text!) // error here
        count += 1
    }
}

【问题讨论】:

    标签: html swift parsing xpath kanna


    【解决方案1】:

    我通过 print(itemName.first!.text!) 修复了错误。

    获取第一个 xml 元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      • 1970-01-01
      • 2023-04-07
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多