UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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....
        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

1      

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

   

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.