[英]How to use Swift Decodable to map relationships from an array of UUID keys?
I have a structure where:我有一个结构,其中:
Factories
have many Cards
; Factories
有很多Cards
;
But also, players
can have many Cards
(not implemented);而且, players
可以拥有许多Cards
(未实施); they are the same cards.他们是同一张牌。
For testing and internal integrity checks;用于测试和内部完整性检查; I'd like to keep the array of cards seperate for now in its own JSON array.我想暂时将卡片阵列单独保存在其自己的 JSON 阵列中。
So the structure I've come up with is like this:所以我想出的结构是这样的:
// 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 }
]
...
}
Note笔记
The Factory JSON structure has no
cards
;工厂JSON结构无cards
; its considered to be an empty array when decoding解码时它被认为是一个空数组
Use case:用例:
Fill the factories cards array (empty at init) with the contents of the references based off the matching UUID keys <根据匹配的 UUID 键,用引用的内容填充工厂卡数组(初始化时为空)<
Swift structs are: 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
}
}
So, to try to do this;因此,尝试这样做; I've wrote out custom initWithCoder
for each struct.我已经为每个结构编写了自定义的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)
}
}
I'd like to do something like this:我想做这样的事情:
// 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")
But I'm not sure what to map $0 against considering the Card struct isn't directly accessible via this struct.但是我不确定 map $0 考虑到 Card 结构不能通过这个结构直接访问。
I am aware I can just put the Cards to sit inside the factories struct as a child array.我知道我可以将卡片作为子数组放在工厂结构内。
But I'm thinking ahead, there might come a time in the future where I need a bridging struct with key references like a one to many through
JSON structure.但我在想,将来可能会有一个时间,我需要一个桥接结构,其中包含关键引用,例如one to many through
JSON 结构的一对多。
I appreciate any feedback offered.我感谢提供的任何反馈。
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.