微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

忽略Swift Codable中的以下属性?

如何解决忽略Swift Codable中的以下属性?

我有这个Account结构。当我发送“ POST”端点并使用它们返回成功解码以快速处理对象时,我想忽略以下属性环境和Id编码到JSON对象中

更新我遇到此错误属性类型“ [环境]”与其包装类型“ SkipEncode”的“ wrappedValue”属性不匹配

struct Account: Codable {
    let accountID,displayName,managedByID,id: String
    let environments: [Environment]
    let contacts: [Contact]

    enum CodingKeys: String,CodingKey {
        case accountID
        case displayName
        case managedID
        case id
        case environments
        case contacts
    }
}

解决方法

如果要在编码时忽略environments,例如,可以定义自己的编码实现:

struct Account: Codable {
    let accountID,displayName,managedByID,id: String
    let environments: [Environment]
    let contacts: [Contact]

    enum CodingKeys: String,CodingKey {
        case accountID,id,environments,contacts
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(accountID,forKey: .accountID)
        try container.encode(displayName,forKey: .displayName)
        // ... just don't include the environments here
    }
}
,

另一种方法,如果您不想手动实现encode(to:)并放弃Codable自动综合的好处,则可以创建一个属性包装器,作为标记所需属性的一种方式跳过:

@propertyWrapper
struct SkipEncode<T> {
   var wrappedValue: T
}

extension SkipEncode: Decodable where T: Decodable {
   init(from decoder: Decoder) throws {
      let container = try decoder.singleValueContainer()
      self.wrappedValue = try container.decode(T.self)
   }
}

extension SkipEncode: Encodable {
   func encode(to encoder: Encoder) throws {
      // nothing to do here
   }
}

extension KeyedEncodingContainer {
   mutating func encode<T>(_ value: SkipEncode<T>,forKey key: K) throws {
      // overload,but do nothing
   }
}

那么您可以像这样使用@SkipEncode

struct Account: Codable {
    let accountID,managedByID: String
    let contacts: [Contact]
    
    @SkipEncode
    let id: String

    @SkipEncode 
    let environments: [Environment]
}
,

按照NewDev建议的方法进行操作。如果要实现属性包装器,则需要实现一个通用的包装器,以便能够跳过任何属性,而不仅仅是字符串:

@propertyWrapper
struct SkipEncode<T: Decodable> {
   var wrappedValue: T
}

extension SkipEncode: Codable {
    init(from decoder: Decoder) throws {
       wrappedValue = try decoder.singleValueContainer().decode(T.self)
    }
    func encode(to encoder: Encoder) throws { }
}

extension KeyedEncodingContainer {
    mutating func encode<T>(_ value: SkipEncode<T>,forKey key: K) throws { }
}

游乐场测试:

struct环境:可编码{} struct联系人:可编码{}

struct Account: Codable {
    let accountID,managedByID: String

    @SkipEncode
    var id: String

    @SkipEncode
    var environments: [Environment]
    
    let contacts: [Contact]
}

let account = Account.init(accountID: "account",displayName: "name",managedByID: "manager",id: "id",environments: [],contacts: [])
let dt = try JSONEncoder().encode(account)
print(String.init(data: dt,encoding: .utf8)!) // {"accountID":"account","contacts":[],"managedByID":"manager","displayName":"name"}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。