Lesson 11 - State Properties... Instances?


In the 14 day challenge on Lesson 11 - State Properties, we begin to learn how to update the views of properties using @State. I see we added 4 @State properties to the top of our view struct because those are variable we want to update the view of.

But what if we wanted to create a new Class called Card, with a struct inside called Card, and when we add cards to the screen, we simply add a new instance of the card? I got the instance working, so when I start my app a random card is selected for the player and the cpu, but how would I update the instances/properties of these cards at runtime and update changes of the cards?

I strongly feel like creating reusable code here is smart by going with a Card Class and I feel that the lesson could have added value and reinforcement in instances by showing us this. Any experts care to chime in and help?

Thank you

Hey Bug,

Thanks for writing! There are definitely different ways to do it but the main priority during this particular series is that non-coders can follow it and get a result.

I find that learners who are just starting out can easily get overwhelmed so I decided to introduce classes later on. Otherwise i would have to explain reference types vs value types and what’s happening in memory. That could easily overwhelm a non-technical person and cause them to give up.

So that’s why we’re going with the most simplistic way to do things as we slowly build up the skill level. By that time, the non-coder would be invested enough to persevere through more abstract concepts.

That said however, I would definitely welcome user generated content from you or the community. There’s immense value in seeing community produced tutorials and different ways to do something. I would link to it from the video description and indicate that there are more advanced ways to accomplish the same result!

Thanks for learning with me and being part of our community :slight_smile:

Hey Chris!

Thanks for the reply on the feedback. Any chance you can briefly touch on how to accomplish this properly here?

Thanks again

Hey Bug,

Were you thinking about creating a reusable Card view?
Or were you talking about creating a class/struct to model the data representation of a card?

I think to model the data representation of a card. I was thinking about creating a separate .swift file called Card, and then storing any properties / methods in there, but being able to utilize it in different .swift files while still having the ability to update properties and view. I guess just for organizing purposes. Not too sure what is best practice here and the pros/cons. I know there’s always multiple ways to go about something and there’s not usually a right answer here but what might you do for reusability?

I’ll keep checking back. I don’t want to progress in the lessons until I’ve jumped this hurdle.

Hey Bug,

That’s definitely the way to go about it if you are intending to build this app out!

In fact, in the next module, I do just that!
I create a Recipe class to represent individual recipes from a data source.

For this war card game, if you’re not going to extend it further then i think it might be overkill.

1 Like