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

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

3      

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.

4      

I got it, Thank for you repley

3      

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!

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.