Happy Monday everyone!
I just finished Day 52 (and all the challenges), and I was ready to move on to Day 53.
But right off the bat, I read the quote "write stupid code that uses smart objects", and felt a slight sense of guilt. So I went back to Day 52 to take a closer look at what we did in terms of data structure and objects.
We implemented the same solution using two different data structures,
#1. Paul's original solution: 1 class
, multiple @Published properties
per class
object and no struct
.
#2. Challenge solution: 1 class
, 1 @Published struct
, and multiple properties inside struct
.
I know that classes are reference types and structs are value types, meaning whenever you modify a class property, Swift changes the original object. When you modify a struct, Swift creates a new struct, which I believe is exactly what we did for the Day 47: Habit-tracking app challenge where we had to copy the existing activity and add 1 to its completion count (because activity was a struct.)
However, for Cupcake Corner, the only "obvious" difference (at least to me) was how convenient it was to use Codable for Solution #2 as I only had to take care of one published struct, as opposed to solution #1.
I'm sure there are MANY other factors to consider when you decide on something like #1 vs. #2, but I just couldn't come up with a practical scenario. Does anyone have some real-world examples of when it would be better to use #1 over #2, and vice versa?