'My Array can be used for one section of my code, but not the other?

I am making a location finder app using SwiftUI, and have my locations hard-coded into an array called 'locations'

Example:

var locations = [

    
    Location(image: "The Ivy", title: "The Ivy", tag: "Bar", price: 3, coordinate: CLLocationCoordinate2D(latitude: -33.86633754527662, longitude: 151.20748914919602), description: "Stylish restaurants, a hip rooftop pool bar & big club nights in a landmark entertainment complex."),
]

struct Location : Identifiable {
    
    var id = UUID().uuidString
    var image : String
    var title : String
    var tag : String
    var price : Int
    var coordinate: CLLocationCoordinate2D
    var description : String
    
}

I have a grid of locations on the home screen that already uses this code to create Detail views of each location using Lazy Grids.

Example:

// GRID...
                    LazyHGrid(rows: Array(repeating: GridItem(.flexible(),spacing: 15), count: 1), spacing: 30){
                        
                        // Sample Locations...
                        ForEach(locations){location in
                            
                            // Location View...
                            LocationGrid(location: location)
                        }
                    }

The issue is that I have a map, which has location pins placed on it which it grabs from the array. But when I try and get those pins to link to a detail view, an error shows saying that "location" was not found in scope. I don't get how I'm using "location" in literally the line above perfectly fine, but its not found when I want to use it again a line later.

It's probably some silly error on my part but any solution would be much appreciated.

Map code:

import SwiftUI
import MapKit
struct MapView: View {
    @State var detail = false
    @Environment(\.presentationMode) var presentationMode:Binding<PresentationMode>
    @State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: -33.8688, longitude: 151.2093), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))

    var body: some View {
        
        VStack {
            ZStack {
                                
                Map(coordinateRegion: $region, annotationItems: locations) {
                    MapAnnotation(coordinate: $0.coordinate) {
                        Button {
                            detail.toggle()
                        } label: {
                            Image("Pin")
                                 .renderingMode(.template)
                                 .resizable()
                             .foregroundColor(.red)
                             
                             .frame(width: 30, height: 30)
                        }
 
                    }
                }
                .frame(width: 420, height: 910)
                .ignoresSafeArea()
                
                HStack {            
            
                    Button(action: {self.presentationMode.wrappedValue.dismiss()}) {
            
                        Image(systemName: "chevron.left")
                           .font(.system(size: 34, weight: .bold))
                           .foregroundColor(Color("Text_LightWhenDark"))
                           .padding(.all, 20)
                           .background(Color("top"))
                           .clipShape(RoundedRectangle(cornerRadius: 25.0))
                
                    }
                    .offset(x: -150, y: -380)
        
                }
                .padding([.horizontal,.top])
        
            }
        }
        .navigationBarTitle("")
        .navigationBarHidden(true)
        
        .fullScreenCover(isPresented: $detail) {
            DetailView(location: location)
        }
    }
}

Error appears at DetailView(location: location)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source