Module 2 Wrap Up Challenge: Movie Tracker App

I face problem when I want to do star in the personal rating can you help me to solve it

Welcome to the community.

The way I approached that was to create a HStack and inside that use a

ForEach(1…5, id: \.self) { value in

Inside that closure test that value and if it is less than or equal to the rating then use an SF Symbol “star.fill” with a forground color of yellow.

See how you go.

1 Like

Can you help me understand this? I haven’t seen this before. I’ve done some searching to try to understand it but I still do not. Do I need to create a new variable and pass it in in the place of value? When I say value in what is the in?
If I make an if statement what values is the if statement comparing? Do I need to set a default value for the Personal Rating?

Any help you can provide would be appreciated.

Hi Justin,

I’ll try to make it sensible for you as I know that anyone new to Swift might find the ForEach construct a little confusing.

Essentially what ForEach does is enable you to Loop over something. That something could be an array of items where it will Loop over the array for each item in the array. The other thing it can do is to Loop a fixed number of times as in ForEach(1...5, .......... where it will start at 1 and finish at 5.

Also note that the ForEach construct is only applicable in a View.

In the case of the following:

ForEach(1...5, id: \.self) { value in

What is happening here is that it will start at 1 and finish at 5 inclusive. The id: \.self means that it will generate an integer value and pass that into the closure where you see value in. value is just a name and it can be any name you like. I could easily have named it like this:

ForEach(1...5, id: \.self) { loopValue in
     // Some code here

The in word is like saying “Loop from 1 to 5 and use the loopValue in the closure”

In the case above the Loop is from 1 to 5 and is an integer value so loopValue is an Integer.

If you were to have an array of animals like this:

let animals = ["ardvaaark", "beaver", "cat", "dog", "elephant", "fox"] 

and you had a ForEach that looped over that array you would say:

ForEach(animals, id: \.self) { animal in 

and in this case the passed in item is a string so animal is a string.

I hope that helps

Thanks so much. Very clear explanation. It helped me finish the project.