Swift 编码:多种 .encode 策略?

huangapple go评论94阅读模式
英文:

Swift encoding: multiple .encode strategeis?

问题

class TestModel {
enum CodingKeys: String, CodingKey {
case someKey = "some_key"
case otherKey = "other_key"
case thirdKey = "third_key"
case apiID = "id"
// ... lots of other keys
}

  1. func encode(to encoder: Encoder) throws {
  2. var container = encoder.container(keyedBy: CodingKeys.self)
  3. try container.encode(someKeyValue, forKey: .someKey)
  4. try container.encode(otherKeyValue, forKey: .otherKey)
  5. try container.encode(thirdKeyValue, forKey: .thirdKey)
  6. // ... lots of other encoded fields
  7. }

}

上述代码是一个名为 TestModel 的模型,用于将数据编码为 JSON 以发送到 API。但有时我希望向不同的终端发送请求,仅更新单个属性。更新始终针对相同的属性。目前,我在 encode() 中发送所有数据,这会导致大量的带宽浪费。

我相信有一种简单的方法可以做到这一点,但文档/谷歌/stackoverflow 并没有提供有用的信息。所以,您对如何创建一个类似下面的第二个编码策略并调用它有什么想法吗?

  1. func encodeForUpdate(to encoder: Encoder) throws {
  2. var container = encoder.container(keyedBy: CodingKeys.self)
  3. try container.encode(apiID, forKey: .apiID)
  4. try container.encode(justOneValueToUpdate, forKey: .someKey)
  5. }
英文:

I have a model TestModel that encodes data to JSON to send to an API. It looks like this:

  1. // Called by: JSONEncoder().encode(testModelObject)
  2. class TestModel {
  3. enum CodingKeys: String, CodingKey {
  4. case someKey = "some_key"
  5. case otherKey = "other_key"
  6. case thirdKey = "third_key"
  7. case apiID = "id"
  8. // ... lots of other keys
  9. }
  10. func encode(to encoder: Encoder) throws {
  11. var container = encoder.container(keyedBy: CodingKeys.self)
  12. try container.encode(someKeyValue, forKey: .someKey)
  13. try container.encode(otherKeyValue, forKey: .otherKey)
  14. try container.encode(thirdKeyValue, forKey: .thirdKey)
  15. // ... lots of other encoded fields
  16. }
  17. }

The above works fine, however sometimes I wish to send a request to a different endpoint that updates just a single attribute. The update is always going to be for the same attribute. At present I'm sending through all data in encode(), which equals a lot of wasted bandwidth.

I'm sure there's an easy way to do this, but docs/google/stackoverflow aren't proving helpful. So: any thoughts on how to create a second encoding strategy along the lines of the below and call it?

  1. func encodeForUpdate(to encoder: Encoder) throws {
  2. var container = encoder.container(keyedBy: CodingKeys.self)
  3. try container.encode(apiID, forKey: .apiID)
  4. try container.encode(justOneValueToUpdate, forKey: .someKey)
  5. }

答案1

得分: 2

需要有一个名为encode(to encoder: Encoder)的单一函数,但您可以通过使用特定的CodingKey枚举来解决这个问题。

  1. enum SimpleCodingKeys: String, CodingKey {
  2. case thirdKey = "third_key"
  3. case apiID = "id"
  4. }

然后使用JSONEncoderuserInfo属性来告诉何时使用这个第二个枚举。首先,我们需要一个键:

  1. extension TestModel {
  2. static var useSimpleCodingKeys: CodingUserInfoKey {
  3. return CodingUserInfoKey(rawValue: "useSimpleCodingKeys")!
  4. }
  5. }

然后调整编码函数:

  1. func encode(to encoder: Encoder) throws {
  2. let useSimple = encoder.userInfo[Self.useSimpleCodingKeys] as? Bool ?? false
  3. if useSimple {
  4. var container = encoder.container(keyedBy: SimpleCodingKeys.self)
  5. try container.encode(apiID, forKey: .apiID)
  6. try container.encode(thirdKeyValue, forKey: .thirdKey)
  7. } else {
  8. var container = encoder.container(keyedBy: CodingKeys.self)
  9. try container.encode(someKeyValue, forKey: .someKey)
  10. try container.encode(otherKeyValue, forKey: .otherKey)
  11. try container.encode(thirdKeyValue, forKey: .thirdKey)
  12. // ...
  13. }
  14. }

当编码时,当然要在字典中设置这个值:

  1. let encoder = JSONEncoder()
  2. encoder.userInfo[TestModel.useSimpleCodingKeys] = true
英文:

You need to have a single encode(to encoder: Encoder) function but you can solve this by using a specific CodingKey enum for the second strategy

  1. enum SimpleCodingKeys: String, CodingKey {
  2. case thirdKey = "third_key"
  3. case apiID = "id"
  4. }

and then use the userInfo property of JSONEncoder to tell when you want to use this second enum. First we need a key to use

  1. extension TestModel {
  2. static var useSimpleCodingKeys: CodingUserInfoKey {
  3. return CodingUserInfoKey(rawValue: "useSimpleCodingKeys")!
  4. }
  5. }

and then adjust the encoding function

  1. func encode(to encoder: Encoder) throws {
  2. let useSimple = encoder.userInfo[Self.useSimpleCodingKeys] as? Bool ?? false
  3. if useSimple {
  4. var container = encoder.container(keyedBy: SimpleCodingKeys.self)
  5. try container.encode(apiID, forKey: .apiID)
  6. try container.encode(thirdKeyValue, forKey: .thirdKey)
  7. } else {
  8. var container = encoder.container(keyedBy: CodingKeys.self)
  9. try container.encode(someKeyValue, forKey: .someKey)
  10. try container.encode(otherKeyValue, forKey: .otherKey)
  11. try container.encode(thirdKeyValue, forKey: .thirdKey)
  12. ...
  13. }
  14. }

And of course set this value in the dictionary when encoding

  1. let encoder = JSONEncoder()
  2. encoder.userInfo[TestModel.useSimpleCodingKeys] = true

huangapple
  • 本文由 发表于 2023年2月6日 05:45:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定