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

SOLVED: whether this is a trailing closure?

Forums > 100 Days of Swift

in apple swiftui tutorials code

struct LandmarkList: View {
    var body: some View {
        NavigationView {
            List(landmarks) { landmark in
                NavigationLink {
                    LandmarkDetail()
                } label: {
                    LandmarkRow(landmark: landmark)
                }
            }
            .navigationTitle("Landmarks")
        }
    }
}

I confuse with NavigationLink. parameter is two closures.but it is not a standard trailing closure

If the last parameter to a function is a closure, Swift lets you use special syntax called trailing closure syntax. Rather than pass in your closure as a parameter, you pass it directly after the function inside braces.

those code also work,but I do not know why

import Cocoa

func TwoClosure( action1:()->String , action2:()->String ){
    print("action1 output:\(action1())");
    print("action2 output:\(action2())");
}

TwoClosure{
    "this is action1"
} action2:{
     "this is action2"
}

func AnotherClosure( action3:()->String , action4:()->String ,action5:()->String ){
    print("\(action3())&\(action4())&\(action5())")
}

AnotherClosure{
   "33333333"
} action4: {
   "444444444"
} action5: {
    "55555555"
}

1      

Since Swift 5.3 (released in Sept 2020), we have been able to use multiple trailing closures. That's what you are seeing here.

From the docs:

If a function takes multiple closures, you omit the argument label for the first trailing closure and you label the remaining trailing closures.

2      

I got it, Thank for you repley

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

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.