2

我正在使用一个协议来创建几个我用来解码的结构JSONDecoder。这是我想要实现的代码示例。

protocol Animal: Codable
{
   var name: String { get }
   var age: Int { get }
}

struct Dog: Animal
{
   let name: String
   let age: Int
   let type: String
}

struct Cat: Animal
{
   let name: String
   let age: Int
   let color: String
}

以下是 dog 和 cat 的单独 JSON 有效负载:

{
    "name": "fleabag",
    "age": 3,
    "type": "big"
}

{
    "name": "felix",
    "age": 2,
    "color": "black"
}

因此,当我解码 JSON 时,我不确定我将拥有什么 JSON,狗还是猫。我试过这样做:

let data = Data(contentsOf: url)
let value = JSONDecoder().decode(Animal.self, from: data)

但最终出现此错误:

在参数类型“Animal.Protocol”中,“Animal”不符合预期的“Decodable”类型

关于解析 dog 或 cat 返回实例的最佳方法的任何想法Animal

谢谢

4

3 回答 3

0

你在这里打开了一个有点丑陋的蠕虫罐头。我理解你试图做什么,但不幸的是它在很多方面都失败了。您可以通过以下 Playground 获得一些接近您想要的东西:

import Cocoa

let dogData = """
{
    "name": "fleabag",
    "age": 3,
    "type": "big"
}
""".data(using: .utf8)!

let catData = """
{
    "name": "felix",
    "age": 2,
    "color": "black"
}
""".data(using: .utf8)!

protocol Animal: Codable
{
    var name: String { get }
    var age: Int { get }
}

struct Dog: Animal
{
    let name: String
    let age: Int
    let type: String
}

struct Cat: Animal
{
    let name: String
    let age: Int
    let color: String
}

do {
    let decoder = JSONDecoder()
    let dog = try decoder.decode(Dog.self, from: dogData)
    print(dog)
    let cat = try decoder.decode(Cat.self, from: catData)
    print(cat)
}

extension Animal {
    static func make(fromJSON data: Data) -> Animal? {
        let decoder = JSONDecoder()
        do {
            let dog = try decoder.decode(Dog.self, from: data)
            return dog
        } catch {
            do {
                let cat = try decoder.decode(Cat.self, from: data)
                return cat
            } catch {
                return nil
            }
        }
    }
}

if let animal = Dog.make(fromJSON: dogData) {
    print(animal)
}
if let animal2 = Dog.make(fromJSON: catData) {
    print(animal2)
}

但是,您会注意到有一些变化是有原因的。事实上,您无法实现该Decodable方法init(from: Decoder) throws,因为它应该适用chaininit......并不能真正适用于协议的方法。我选择在该Animal.make方法中实现您最喜欢的调度程序,但这最终也成为了一个半生不熟的解决方案。由于protocols是元类型(可能也有充分的理由),因此您无法在元类型上调用它们的静态方法,而必须使用具体的方法。正如这条线Dog.make(fromJSON: catData)所示,至少可以说这看起来很奇怪。最好将其烘焙到顶级功能中,例如

func parseAnimal(from data:Data) {
    ...
}

但这仍然以另一种方式看起来并不令人满意,因为它污染了全局命名空间。可能仍然是我们可以用可用的手段做的最好的事情。

鉴于调度程序的丑陋,使用没有直接指示类型的 JSON 似乎是个坏主意,因为它使解析变得非常困难。但是,我看不到以真正易于解析的方式在 JSON 中传达子类型的好方法。尚未对此进行任何研究,但这可能是您的下一次尝试。

于 2018-05-01T16:24:39.960 回答
0

更好的方法是使用类而不是协议,并使用类而不是结构。你的DogCat类将是的子类Animal

class Animal: Codable {
    let name: String
    let age: Int

    private enum CodingKeys: String, CodingKey {
        case name
        case age
    }
}

class Dog: Animal {
    let type: String

    private enum CodingKeys: String, CodingKey {
        case type
    }

    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.type = try container.decode(String.self, forKey: .type)
        try super.init(from: decoder)
    }
}

class Cat: Animal {
    let color: String

    private enum CodingKeys: String, CodingKey {
        case color
    }

    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.color = try container.decode(String.self, forKey: .color)
        try super.init(from: decoder)
    }
}

let data = Data(contentsOf: url)
let animal = JSONDecoder().decode(Animal.self, from: data)
于 2018-05-01T04:05:14.757 回答
0

您将无法使用它:

let animal = try? JSONDecoder().decode(Animal.self, from: data)

解码狗或猫。它永远是动物。

如果您想将这两个 JSON 对象解码为 Animal,则像这样定义 Animal:

struct Animal: Codable {
    var name: String
    var age: Int
}

当然,您将失去使它们成为 Dog ( type) 或 Cat ( color) 的独特元素。

于 2018-05-01T04:55:46.620 回答