From the course: iOS Development Tips

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

ForEach closures in SwiftUI

ForEach closures in SwiftUI

From the course: iOS Development Tips

ForEach closures in SwiftUI

- [Instructor] While you might defer to List, ForEach is a powerful way to iterate collections in SwiftUI, giving you the power to customize where lists constrain you. Let's do a little deep dive into ForEach. If you download the exercise file you'll find a simple example. I have a model that conforms to identifiable up here. I'm going to make each item an item and show the name on a device in a text view. One shortcut on smaller loops is the item isn't necessary. Now, this here is the closure, if you cmd + ctrl + click on the ForEach you'll go to the definition. In the definitions, you'll see here the closure. And you'll see that it's got a parameter for data element. Closures have a shorthand for this, if you don't want to list them. So, go back to our code, and I'm going to get rid of item here, and I'm going to get rid of it here, and change it to $0. A digit prefixed by $ is an indicator of the closure's parameter.…

Contents