代码之家  ›  专栏  ›  技术社区  ›  blue

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

  •  0
  • blue  · 技术社区  · 6 年前

    好吧,我要下班了 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中不存在键(即pub date)时阻止解析器出错。在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
        }
    

    一堆这些。当它说“没有与密钥关联的密钥”时是否有人遇到问题/抛出错误?我怎么能用这个图书馆来解决这个问题?

    1 回复  |  直到 6 年前
        1
  •  1
  •   S.Moore    6 年前

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

    这类似于如果json中不存在该值,您将在苹果的jsondecoder中发现的行为。


    在你的例子中,你提到 pubDate 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"
        }
    }
    

    所以在这种情况下 脓疮 确实存在于XML中,该属性中将有日期。如果XML中不存在密钥,则 脓疮 nil .

    您可以在结构中选择任意多个属性,它们都将遵循此模式。