繁体   English   中英

如何使用 Swift 从一组 UUID 键中解码为 map 关系?

[英]How to use Swift Decodable to map relationships from an array of UUID keys?

我有一个结构,其中:

Factories有很多Cards

而且, players可以拥有许多Cards (未实施); 他们是同一张牌。

用于测试和内部完整性检查; 我想暂时将卡片阵列单独保存在其自己的 JSON 阵列中。

所以我想出的结构是这样的:

// Example JSON
{
    "factories": [
        {
            "id": "7a24a713-c0aa-4498-....",
            "name": "Steel Factory",
            "cost": 4,
            "references": [
                { "id": "e936b897-c203-44a5-9cf0-27720f655b68" },
                { "id": "da35424a-6546-4c72-a51c-1f6abfd601a3" }
            ],
     ...
     "cards": [
          { "id": "e936b897-c203-44a5-9cf0-27720f655b68", name: "Card #1", cost: 4 }
          { "id": "da35424a-6546-4c72-a51c-1f6abfd601a3", name: "Card #2", cost: 4 }
     ] 
...
}

笔记

工厂JSON结构无cards 解码时它被认为是一个空数组


用例:

根据匹配的 UUID 键,用引用的内容填充工厂卡数组(初始化时为空)<


Swift 结构是:

struct Factory: Codable, Identifiable, Equatable, Hashable {
    let id: UUID
    let name: String
    let cost: Int
    var cards: [Card]? 
    internal var references: [Reference]?

    private enum CodingKeys: String, CodingKey {
        case id, name, cost, references
    }
}

struct Reference: Codable, Identifiable, Hashable, Equatable {
    var id: UUID

    private enum CodingKeys: String, CodingKey {
        case id
    }
}

struct Card: Codable, Identifiable, Hashable, Equatable {
    var id: UUID 
    var name: String
    var cost: Int

    private enum CodingKeys: String, CodingKey {
        case id, name, cost
    }
}

因此,尝试这样做; 我已经为每个结构编写了自定义的initWithCoder

extension Reference {
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        id = try container.decode(UUID.self, forKey: .id)
    }
}


extension Factory {

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        id = try container.decode(UUID.self, forKey: .id)
        name = try container.decode(String.self, forKey: .name)
        cost = try container.decode(Int.self, forKey: .cost)
        cards = try container.decodeIfPresent([Card].self, forKey: .cards) ?? [Card]()
        references = try container.decode([Reference].self, forKey: .references)
    }
}

extension Card {
   init(from decoder: Decoder) throws {
       let container = try decoder.container(keyedBy: CodingKeys.self)

       id = try container.decode(UUID.self, forKey: .id)
       name = try container.decode(String.self, forKey: .name)
       cost = try container.decode(String.self, forKey: .cost)

   }
}

我想做这样的事情:

// Factory decoder 
references = try container.decode([Reference].self, forKey: .references)

// Find all cards that match the reference ID
// add the matched cards to this factories cards array
let matchedCards: [Card]? = references.map { $0  }
print ("matchedCards")

但是我不确定 map $0 考虑到 Card 结构不能通过这个结构直接访问。

我知道我可以将卡片作为子数组放在工厂结构内。

但我在想,将来可能会有一个时间,我需要一个桥接结构,其中包含关键引用,例如one to many through JSON 结构的一对多。

我感谢提供的任何反馈。

let referenceIds:[String] = references.map { $0.id }

let decodedCards = container.decode([Card].self, forKey: .cards)

let matchedCards[Card] = decodedCards.filter(where: { referenceIds.contains($0.id)})

self.cards = matchedCards // factory.cards

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM