自定义XML解析器 - 当某些键不存在时该怎么办?

时间:2018-06-06 20:36:33

标签: ios swift xml nsxmlparser

好的,我正在处理Implementing a custom Decoder in Swift 4,然后解析器在XML中查找这些键:

struct RSSChannel: Codable {
    var title: String
    var pubDate: Date
    var link: URL
    var description: String
    var items: [RSSItem]
    enum CodingKeys: String, CodingKey {
        case title, pubDate, link, description
        case items = "item"
    }
}'

(我没有写这个,这是GitHub所以我在这里挣扎) -

我需要知道如果在Feed中不存在密钥,即发布日期,如何停止解析器错误。在XMLDecoder类中,有所有这些解码器:

 public func decode(_ type: UInt.Type) throws -> UInt {
        try expectNonNull(UInt.self)
        return try self.unbox(self.storage.topContainer, as: UInt.self)!
    }

    public func decode(_ type: UInt8.Type) throws -> UInt8 {
        try expectNonNull(UInt8.self)
        return try self.unbox(self.storage.topContainer, as: UInt8.self)!
    }

    public func decode(_ type: UInt16.Type) throws -> UInt16 {
        try expectNonNull(UInt16.self)
        return try self.unbox(self.storage.topContainer, as: UInt16.self)!
    }

    public func decode(_ type: UInt32.Type) throws -> UInt32 {
        try expectNonNull(UInt32.self)
        return try self.unbox(self.storage.topContainer, as: UInt32.self)!
    }

    public func decode(_ type: UInt64.Type) throws -> UInt64 {
        try expectNonNull(UInt64.self)
        return try self.unbox(self.storage.topContainer, as: UInt64.self)!
    }

    public func decode(_ type: Float.Type) throws -> Float {
        try expectNonNull(Float.self)
        return try self.unbox(self.storage.topContainer, as: Float.self)!
    }

    public func decode(_ type: Double.Type) throws -> Double {
        try expectNonNull(Double.self)

实际上是:

  public func decode<T : Decodable>(_ type: T.Type, forKey key: Key) throws -> T {
        guard let entry = self.container[key.stringValue] else {

            print("SKYaw")
            throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))
        }

        self.decoder.codingPath.append(key)
        defer { self.decoder.codingPath.removeLast() }

        guard let value = try self.decoder.unbox(entry, as: type) else {
            throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
        }

        return value
    }

其中一堆。当有人说“没有与Key相关的密钥”/抛出错误时,有没有人遇到问题?我怎样才能使用这个库来解决这个问题?

1 个答案:

答案 0 :(得分:1)

在XMLParsing库中,如果属性可能不存在于数据中,则需要使该属性成为可选项。

这与您在Apple的JSONDecoder中找到的行为类似,如果JSON中不存在该值。

在您的示例中,您提到XML中可能不存在pubDate,因此更新后的频道结构将显示如下:

struct RSSChannel: Codable {
    var title: String
    var pubDate: Date?
    var link: URL
    var description: String
    var items: [RSSItem]

    enum CodingKeys: String, CodingKey {
        case title, pubDate, link, description
        case items = "item"
    }
}

因此,在XML中存在pubDate的情况下,该属性中将存在日期。如果XML中不存在密钥,pubDate的值将为nil

您可以在结构中创建任意数量的属性,并且它们都将遵循此模式。