XMLCoder Documentation

Protocol Dynamic​Node​Encoding

public protocol DynamicNodeEncoding: Encodable  

Allows conforming types to specify how its properties will be encoded.

For example:

struct Book: Codable, Equatable, DynamicNodeEncoding {
    let id: UInt
    let title: String
    let categories: [Category]

    enum CodingKeys: String, CodingKey {
        case id
        case title
        case categories = "category"
    }

    static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
        switch key {
        case Book.CodingKeys.id: return .both
        default: return .element
        }
    }
}

produces XML of this form for values of type Book:

<book id="123">
    <id>123</id>
    <title>Cat in the Hat</title>
    <category>Kids</category>
    <category>Wildlife</category>
</book>
%3 DynamicNodeEncoding DynamicNodeEncoding Encodable Encodable DynamicNodeEncoding->Encodable

Conforms To

Encodable

Default Implementations

node​Encoding(for:​)

static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding  

Requirements

node​Encoding(for:​)

static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding