GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SOLVED: Trailing closure on the View protocol

Forums > SwiftUI

I’ve completed the Swift part of the 100 Days of SwiftUI and am on to the first project; but I have a question about the SwiftUI View protocol.

My question is around the trailing closure in, say, this code:

var body: some View {
    NavigationView {
        Form {
            Picker("Select your student", selection: $selectedStudent) {
                ForEach(students, id: \.self) {
                    Text($0)
                }
            }
        }
    }
}

Now, I think I have trailing closures understood in my head, but I am not understanding here is what it belongs to. body is a var, which is some sort of View, and then a closure immediately follows.

So far, I have understood this to mean that the last parameter of an initialiser is a closure, here expressed as a trailing closure. Is that closure a part of the View protocol somehow? I did look at the source but couldn’t work it out. Perhaps a protocol extension?

2      

https://www.hackingwithswift.com/books/ios-swiftui/understanding-the-basic-structure-of-a-swiftui-app#:~:text=Third%2C%20var%20body%3A%20some%20View%20defines%20a%20new,to%20the%20View%20protocol%2C%20which%20is%20our%20layout.

Third, var body: some View defines a new computed property called body, which has an interesting type: some View. This means it will return something that conforms to the View protocol, which is our layout. Behind the scenes this will actually result in a very complicated data type being returned based on all the things in our layout, but some View means we don’t need to worry about that.`

body is a computed property and is not using a closure. You're saying there's a body property that is of type some View and it's value is a computed one not an assigned one like:

let someInt = 25

a simpler computed property:

var computedInt: Int {
    5 * 5
}

and now body as a computed property:

var body: some View {
    Text("Hello, world!)
}

The picker is an example of where you are using a trailing closure: Picker(titleKey: LocalizedStringKey, selection: Binding< >, content: () -> _)

You specify the first two values and then provide a closure for the content:

Picker("Select your student", selection: $selectedStudent) {
   ForEach(students, id: \.self) {
        Text($0)
   }
}

The same with ForEach. It also has a trailing closure.

see also: https://www.donnywals.com/what-are-computed-properties-in-swift-and-when-should-you-use-them/#:~:text=A%20computed%20property%20in%20Swift%20is%20a%20property,a%20very%20basic%20example%20of%20a%20computed%20property%3A

3      

The trailing closure is just executable code, and in this case, it is the content paramter if you had put the whole function in longhand.

Same as this:

      Picker("title goes here", selection: $selectedStudent, content: {
          ForEach(students, id:\.self){
              Text($0)
          }
      })

2      

Hi,

Can I ask in @vtabmow comment the syntax is like below? What does ->_ mean? I thought underscore is for omitting the parameter name, here it looks like a return type.

The picker is an example of where you are using a trailing closure:

Picker(titleKey: LocalizedStringKey, selection: Binding< >, content: () -> _)

Thanks Bin

2      

Go further, faster with the Swift Career Accelerator.

GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios.

Learn more here

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.