M2 L16 Quotes Challenge


What is “Hashable” ? it asks me to conform to Hashable which was not covered in the lessons?
Isn’t \.self supposed to do the trick?

have you checked the exercise solution?

Mike,

Going back a few lessons (Lesson 7 at the 13:20 timestamp) Chris talked about adding Identifiable to a struct so that ForEach was able to uniquely identify each element in the array. In that lesson it was done by adding var id = UUID() to the struct.

In your case you have declared that as an Optional, ie you have var id: UUID?, so when you load the json you need to loop through the quote array and populate that id: with a instance of UUID()

You can do that in your json decoding method through a simple loop such as:

for index in 0..<quote.count {
    quote[index].id = UUID()
}

It means that rather than saying this:

ForEach(model.quote, id: \.self) { item in

you can now say this:

ForEach(model.quote) { item in

Also another point you might want to consider in your naming convention.

In your view model it would be better to define the array as quotes (plural) so that in your ForEach statement you could say:

ForEach(model.quotes) { quote in

So you are dealing with an array of quotes and then within the ForEach loop you are referring to a quote.

Does that make sense?

Thank you! It makes so much sense now.

Btw, I did use plural in the ForEach;)

Oh you did too. I see that now on closer inspection.