I have an array of books aka struct Book in the Top100Model below. I also have an array of books aka struct Data as well in the ISBNModel below. Both arrays have the same number of books and have a common property isbn13 which is a string holding a number. Id like to create a dictionary to merge the arrays so that it contains all the properties from Book in the Top100Model but add another two properties from the ISBNModel and copies the ‘subjects’ and ‘synopsis’ properties from the ISBNModel to this dictionary based on the matching isbn13 values. In trying the code below I’m getting a ‘Generic struct Dictionary requires that Book conforms to Hashable’. If I add the Hashable identifier to Book then it gives an error that it must be Equatable also
@State var bookFromTop100Fiction = [Book]()
@State var bookFromISBNDBbyISBN13 = [Data]()
var myDictionary = Dictionary(uniqueKeysWithValues: zip(
bookFromTop100Fiction, bookFromISBNDBbyISBN13))
print(myDictionary)
Top100Model below
struct Top100Model: Decodable {
let results: Result
private enum CodingKeys: String, CodingKey {
case results
}
}
struct Result: Decodable {
let listsb: [ListB]
private enum CodingKeys: String, CodingKey {
case listsb = "lists"
}
}
struct ListB: Codable {
let books: [Book]
}
struct Book: Codable, Identifiable {
let id = UUID()
let author: String
let description: String
let isbn13: String
let rank: Int
let title: String
}
ISBNModel Below
struct ISBNModel: Codable {
let total: Int
let requested: Int
let data: [Data]
}
struct Data: Codable {
let synopsis: String
let subjects: [String]
let isbn13: String
1