Question about code in SwiftUI tutorials

I am novice at apple programming, the following code in SwiftUI tutorials is confusing to me , where can I find the explaination about the syntax?

In section 5,Make the List Dynamic

        List(landmarks, id: \.id) { landmark in
            LandmarkRow(landmark: landmark)
        }
Accepted Answer

That's the very purpose of the tutorial to explain syntax… But in some places, explanations are a bit short.

For this:

List(landmarks) { landmark in
                LandmarkRow(landmark: landmark)
}
  • you will list all items in landmarks (an array)
  • each item is identified in the closure by the landmark var
  • and you execute code that follows in keyword (refer to Swift Programming Language book in Apple Bookstore to get more details)
  • namely, you load the LandMarkRow for this landmark and display in the List.

Hope that helps.

A List shows a row for each item you supply, in a single column:

List {
    Text("A List Item")
    Text("A Second List Item")
    Text("A Third List Item")
}

Because the example is using a list of Landmark objects, they have to be Identifiable, so there's an id parameter for each one.

The syntax is saying:

  • Go through each item in the set of Landmark objects: landmarks
  • Create a variable we can refer to when we're using each one: landmark
  • Add a row using the LandmarkRow view, and supply the current landmark to that view so the view can use the data.

In Xcode if you put your cursor onto a variable, you'll see where else that variable is used, so if you click on landmark on the first line of code you shared, it would be highlighted in the second line, too:

List(landmarks, id: \.id) { >->->landmark<-<-< in
    LandmarkRow(landmark: >->->landmark<-<-<)
}
Question about code in SwiftUI tutorials
 
 
Q