Context: I'm following the tutorial on https://developer.apple.com/tutorials/swiftui/ to get an introduction to Swift and iOS.
In learning about closure notation in Swift, I wanted to write the more expanded version of a closure I came across.
ForEach(filteredLandmarks) {
landmark in
NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
LandmarkRow(landmark: landmark)
}
}
The first code snippet works great.
From what I understand about trailing closures, is that the type of the function needed as the last argument to the ForEach
struct is implies the type of the function defined by the closure, hence we don't need to explicitly say the type of the function defined by the closure and refer only to it's one and only argument landmark
implicitly. But what is the type if I wanted to explicitly define it?
The following is my attempt, which produces the error in Xcode
value of protocol type 'View' cannot conform to 'View'; only struct/enum/class types can conform to protocols
ForEach(filteredLandmarks) {
(landmark: Landmark) -> View in
NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
LandmarkRow(landmark: landmark)
}
}
I know landmark
has a type of Landmark
, because removing the -> View
works perfectly. So really, all I need to know is what is the return type?