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

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?

   

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

   

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)
          }
      })

   

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

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.