gpt4 book ai didi

ios - Swift XMLMapper 解码嵌套属性

转载 作者:可可西里 更新时间:2023-11-01 01:08:28 29 4
gpt4 key购买 nike

使用来自 here 的库.这是详细信息,

XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<status code="25">Verification required</status>
<parsed-challenge>
<action type-id="11">
<url content-type="application/x-www-form-urlencoded" method="POST" type-id="1">https://example.com</url>
<hidden-fields>
<apiRequest>MIAGCSqGSIb3DQEHA6CAMIACAQAxggFAMIIBPAIBAD</apiRequest>
</hidden-fields>
</action>
</parsed-challenge>
<return-url>https://example.com</return-url>
</root>

结构:

struct Secure: XMLMappable {

internal(set) var statusCode: Int?
internal(set) var status: String?
internal(set) var actionType: Int?
internal(set) var url: URLInfo?
internal(set) var hiddenFields: [String:String]?
internal(set) var returnURL: Foundation.URL?

public var nodeName: String!

public init(map: XMLMap) {
statusCode = map.value()
status = map.value()
actionType = map.value()
url = map.value()
hiddenFields = map.value()
returnURL = map.value()
}

public mutating func mapping(map: XMLMap) {
statusCode <- map["status"].attributes["code"]
status <- map["status"].innerText
actionType <- map["parsed-challenge.action"].attributes["type-id"]
url <- map["parsed-challenge.action.url"]
hiddenFields <- map["parsed-challenge.action.hidden-fields"]
returnURL <- (map["return-url"], XMLURLTransform())
}
}

解码时,

Secure(statusCode: nil, status: nil, actionType: nil, url: Optional(URLInfo(url: Optional(https://example.com), method: Optional("POST"), contentType: Optional("application/x-www-form-urlencoded"), typeId: Optional(1))), hiddenFields: Optional(["__name": "hidden-fields", "apiRequest": "MIAGCSqGSIb3DQEHA6CAMIACAQAxggFAMIIBPAIBAD"]), returnURL: Optional(https://example.com))

statusstatusCodeactionType 有什么问题?为什么他们不解码,是因为无法进行深度嵌套解码吗?

最佳答案

我希望我的图书馆能够像那样工作。 (这样嵌套值的映射非常干净)但是目前很难映射嵌套属性和内部具有其他元素或属性的元素的 innerText

因此,建议的映射 XML 的模型如下所示:

struct Secure: XMLMappable {
public var nodeName: String!

internal(set) var status: Status?
internal(set) var action: Action?
internal(set) var returnURL: Foundation.URL?

public init(map: XMLMap) { }

public mutating func mapping(map: XMLMap) {
status <- map["status"]
action <- map["parsed-challenge.action"]
returnURL <- (map["return-url"], XMLURLTransform())
}
}

struct Status: XMLMappable {
public var nodeName: String!

internal(set) var statusCode: Int?
internal(set) var message: String?

public init(map: XMLMap) { }

public mutating func mapping(map: XMLMap) {
statusCode <- map.attributes["code"]
message <- map.innerText
}
}

struct Action: XMLMappable {
public var nodeName: String!

internal(set) var actionType: Int?
internal(set) var url: URLInfo?
internal(set) var hiddenFields: [String:String]?

public init(map: XMLMap) { }

public mutating func mapping(map: XMLMap) {
actionType <- map.attributes["type-id"]
url <- map["url"]
hiddenFields <- map["hidden-fields"]
}
}

struct URLInfo: XMLMappable {
public var nodeName: String!

internal(set) var contentType: String?
internal(set) var method: String?
internal(set) var typeID: Int?
internal(set) var url: Foundation.URL?

public init(map: XMLMap) { }

public mutating func mapping(map: XMLMap) {
contentType <- map.attributes["content-type"]
method <- map.attributes["method"]
typeID <- map.attributes["type-id"]
url <- (map.innerText, XMLURLTransform())
}
}

虽然我个人使用(因为我知道如何破解我的图书馆)我可以使用这样的东西:

struct Secure: XMLMappable {
public var nodeName: String!

internal(set) var statusCode: Int?
internal(set) var status: String?
internal(set) var actionType: Int?
internal(set) var url: URLInfo?
internal(set) var hiddenFields: [String:String]?
internal(set) var returnURL: Foundation.URL?

public init(map: XMLMap) { }

public mutating func mapping(map: XMLMap) {
statusCode <- map["status._code"]
status <- map["status.__text"]
actionType <- map["parsed-challenge.action._type-id"]
url <- map["parsed-challenge.action.url"]
hiddenFields <- map["parsed-challenge.action.hidden-fields"]
returnURL <- (map["return-url"], XMLURLTransform())
}
}

struct URLInfo: XMLMappable {
public var nodeName: String!

internal(set) var contentType: String?
internal(set) var method: String?
internal(set) var typeID: Int?
internal(set) var url: Foundation.URL?

public init(map: XMLMap) { }

public mutating func mapping(map: XMLMap) {
contentType <- map.attributes["content-type"]
method <- map.attributes["method"]
typeID <- map.attributes["type-id"]
url <- (map.innerText, XMLURLTransform())
}
}

两种模型都可以正常工作。

如果嵌套映射在下一个版本中得到改进,我一定会更新这篇文章。

关于ios - Swift XMLMapper 解码嵌套属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52163370/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com