【问题标题】:Issue in converting xml to custom type using swxmlhash使用 swxmlhash 将 xml 转换为自定义类型的问题
【发布时间】:2017-01-25 10:08:50
【问题描述】:

我正在尝试转换以下 xml 标签:

<MTag media="wifi">122</MTag>

转换为等效类型,但无法从提供的示例中找到任何方法。

试过了,

struct Profile: XMLIndexerDeserializable {
// some other elements...
    let MTag: MTagElement

    static func deserialize(_ node: XMLIndexer) throws -> Profile{
         return try SMCPreferenceProfile(
            updateInterval: node["MTag"].value()
         )
    }
}

相应地,

struct MTagElement: XMLIndexerDeserializable {
    let media: String
    let value: Int

    static func deserialize(_ node: XMLIndexer) throws -> MTagElement{
        return try MTagElement(
            media: node.value(ofAttribute: "media"),
            value: node["MTag"].value()
        )
    }
}

这无论如何都是错误的。有什么方法可以将下面的标签转换成等效的自定义类型?

【问题讨论】:

  • 您的 XML 中的"UpdateInterval" 在哪里?我在&lt;MTag media="wifi"&gt;122&lt;/MTag&gt; 的示例 XML 标记中没有看到任何相关内容。
  • 编辑问题

标签: ios xml swift3 swxmlhash


【解决方案1】:

我正在尝试将属性值放在单独的属性中。

struct Profile: XMLIndexerDeserializable {
// some other elements...
    let mTag: Int
    let mTagMedia: String

    static func deserialize(_ node: XMLIndexer) throws -> Profile{
         return try SMCPreferenceProfile(
            mTag: node["MTag"].value(),
            mTagMedia: node["MTag"].value(ofAttribute: "media")
         )
    }
}

如果有其他方法可以正确解决这个问题,请指教。

【讨论】:

    猜你喜欢
    • 2013-05-15
    • 1970-01-01
    • 2022-01-22
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多