List
is a view builder. It's a factory.
For each and every item inside of a collection it will shove that item through some template and gronk out a single item to display on the screen.
So for every entree item inside a restaurant's menu collection, the List
will spit out a single line formatted to your specifications.
For every team in a bowling league, the List
will lay out the team's name and total score.
For each beer in a pub app, the List
will show a frothy photo, and the price per pint.
The List
command just takes the entire collection and builds a smaller view using the content of the objects in the collection, one at a time.
This is the same as the ForEach()
function as well.
// This is a view factory! What kind of views is it going to make?
ForEach(0..<3) { number in
Button { // <----- Here's the answer! This ForEach factory will make Buttons! A valid type of view....
flagTapped(number)
} label: {
Image(countries[number])
.renderingMode(.original)
.clipShape(Capsule())
.shadow(radius: 50)
}
}
user in? beer in?
In your code example, you wrote
// you're using the variable user to receive the object passed in by the List command
List(users, id:\.self, selection: $selection) { user in Text(user) }
// This may confuse you, if you also have a struct in your application named user.
// Instead, you can call the receiver variable anything you want. Try this variation:
List(users, id:\.self, selection: $playerOne) { gamePlayer in Text(gamePlayer.avatarName) }
In this snip, you have a collection of users
and you've coded user
as the receiver's name. But in the List
, you can call the receiver variable by any name you wish.
Keep Coding