I'm a bit new to swift and I made an app where a welcome page would take you to a map where you can add locations and another link that takes you to the sightings of endangered animals website. On the Navigation Link to the map screen, a "Trailing closure passed to the parameter of type 'MKCoordinateRegion' that does not accept a closure" error shows up and the link wouldn't work. Here's my code:
NavigationLink (
"Lets Go!", destination: SwiftUIView()
{
Text("Lets Go!").foregroundColor(.purple)
}).position(x: 165, y: 65)
And here's my screen
struct SwiftUIView: View {
@State private var locations =
[Location]()
@State private var input = ""
@State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 40.7, longitude: -90), span: MKCoordinateSpan(latitudeDelta: 40, longitudeDelta: 40))
var body: some View {
ZStack{
Image("Eco").resizable().aspectRatio(contentMode:.fit).frame(width: 75.0, height: 75.0).position(x: 155, y: 30)
Text("EcoBuild")
Text("Add your own sightings!")
Map(coordinateRegion: $region,annotationItems: locations){
location in MapMarker(coordinate: CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude))
}.frame(width: 330.0, height: 400.0).position(x: 160, y: 350)
Link("See other sightings here!", destination:URL(string:"https://www.mass.gov/info-details/rare-species-viewer")!).position(x:150 , y: 75.0)
Button {
let newLocation = Location(id: UUID(), name: "New Location", description: "Animals in area", latitude: region.center.latitude, longitude: region.center.longitude)
locations.append(newLocation)
} label: {
Image(systemName: "plus")
}
}
}
The compiler isn't giving you a very helpful error message here, but the problem is that the parameters if your
NavigationLink
initializer aren't quite valid. You're trying to pass a title, then a destination, then you're missing a parenthesis (which is the root cause of the funny compiler error -- it thinks that your label is a trailing closure for theSwiftUIView
initializer), then you pass a label.What you probably want is this instead: