Could someone tell me what’s the difference between these two? Thanks.
They both work.
I wrote the first one based on the tutorial of lesson 12
However, in the solution of lesson 12 challenge, it provided another syntax.
for p in pizzaData {
p.id = UUID()
}
for index in 0..<pizzaData.count {
pizzaData[index].id = UUID()
}
If you left your model Pizza as a class then both will work. If you changed it to a struct (which is what Chris did in his solution) then only the second one will work.
The reason that the first one does not work on a struct is that when you use for p in pizzaData {
the p
is a let constant
and let constants are immutable
meaning they cannot be changed.
For some reason, leaving as a struct never worked for me (even with using the indexed loop). Changing to class did however work with both, as you said.