WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Something I've Never Understood About Closures

Forums > 100 Days of SwiftUI

Hi All,

I'm wondering if someone can explain something to me about closures that I can functionally utilize, but have never been able to understand in concept.

When creating a closure we pass parameters to the function and seperate parameters from the function using the word in. However, I don't understand how Swift is able to identify the value we're assigning to the parameter. For example:

List(users, id:\.self, selection: $selection) { user in Text(user) }

We're asking the list view to iterate for each of the users contained within the array. How does swift know that the users values are the parameters we're passing for user? I'm sure this is way simpler in my head and I'm overcomplicating things mentally, but I'd like to understand this part.

Thanks, Andy


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....
    } label: {
            .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


This is a great explanation and pretty much answered the root of the question. Thank you for taking the time to explain it!


Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.