如何使用可解码协议为具有不同密钥的相同 Json 属性创建通用 class

How to use Decodable Protocol to create common class for same Json properties with different keys

{
    "actions" : {
        "upvote" : {
            "delete" : true,
            "read" : true,
            "create" : true,
            "update": true
        },
        "read" : {
            "delete" : true,
            "update" : true,
            "read" : true,
            "create" : true
        }
    }
}

我收到来自服务器的 Json 响应,下面是使用 Decodable 协议创建的模型结构

struct Actions: Decodable {
    let upvote: UpvoteStatus
    let read: ReadStatus

    enum CodingKeys: String, CodingKey {
        case upvote
        case read
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.upvote = try container.decode(UpvoteStatus.self, forKey: .upvote) {
        self.read = try container.decode(ReadStatus.self, forKey: .read)
    }
}

struct UpvoteStatus: Decodable {
    let delete: Bool
    let update: Bool
    let read: Bool
    let create: Bool
}

struct ReadStatus: Decodable {
    var delete: Bool
    var update: Bool
    var read: Bool
    var create: Bool
}

这很好用,但是创建了很多重复代码,因为 UpvoteStatus 和 ReadStatus 结构具有相似的属性,并且来自服务器的 JSON 除了不同的键之外是相似的。

有什么方法可以创建一个通用状态结构,将状态 属性 添加到 ReadStatus 和 UpvoteStatus class

struct Status: Decodable {
    let delete: Bool
    let update: Bool
    let read: Bool
    let create: Bool
} 

现在我想要像下面这样的东西,这样我就可以删除重复的代码。

struct UpvoteStatus: Decodable {
    let status: Status
}

struct ReadStatus: Decodable {
    let status: Status
}

可能这就是你所需要的,也许你想多了:

struct Actions: Decodable {
    let upvote: Status
    let read: Status

    enum CodingKeys: String, CodingKey {
        case upvote
        case read
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.upvote = try container.decode(Status.self, forKey: .upvote) {
        self.read = try container.decode(Status.self, forKey: .read)
    }
}

struct Status: Decodable {
    let delete: Bool
    let update: Bool
    let read: Bool
    let create: Bool
} 

无需编写自定义初始化程序和显式定义编码键。

你可以简单地写

struct Response: Codable {

    let actions: Actions

    struct Actions : Codable {
        let upvote: Element
        let read: Element

        struct Element: Codable {
            let delete: Bool
            let read: Bool
            let create: Bool
            let update: Bool
        }
    }
}