1

I have to pass these data to my MapView and I keep getting the following error: Type '()' cannot conform to 'View'

I need different data for the 3 cases so I thought I will set it up just before passing ig through the NavigationLink, but apparently I can't do so. How can I manage this?

var body: some View {
        NavigationView{
                VStack{
                        HStack{
                                annotations = Locs(name: "Sultan Hotel", coord: CLLocationCoordinate2D(latitude: 5.55739, longitude: 95.3208), alarm: 0, state: 0, radius: 1)
                                
                        NavigationLink(destination: MapView()){
                                Text("Sultan Hotel").foregroundColor(.black)
                                        .font(.system(size:25))
                                        .fontWeight(.bold)
                                        .padding()
                        }
                                Spacer()
                                        if (annotations.state == 1 ) { Text("ON   ")}
                                        else if (annotations.state == 0 ) { Text ("OFF") }
                        
                        }
                        HStack{
                        NavigationLink(destination: MapView()){
                                Text("Paparon Pizza Lhokseumawe").foregroundColor(.black)
                                        .font(.system(size:25))
                                        .fontWeight(.bold)
                                        .padding()
                        }
                                Spacer()
                                if (state == 1 ) { Text("ON   ")}
                                else if (state == 0 ) { Text ("OFF") }
                        }
                        HStack{
                        NavigationLink(destination: MapView()){
                                Text("Pocut Baren").foregroundColor(.black)
                                        .font(.system(size:25))
                                        .fontWeight(.bold)
                                        .padding()
                        }
                                Spacer()
                                if (state == 1 ) { Text("ON   ")}
                                else if (state == 0 ) { Text ("OFF") }
                        }
                        Spacer()
                }.navigationTitle("Geofence")
        }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
Jakabffy
  • 19
  • 1
  • you cannot have code like this in a view: `annotations = Locs(name: "Sultan Hotel", coord: CLLocationCoordinate2D(latitude: 5.55739, longitude: 95.3208), alarm: 0, state: 0, radius: 1)` You should review this basic tutorial: https://developer.apple.com/tutorials/swiftui/ – workingdog support Ukraine Jan 02 '22 at 11:34

1 Answers1

1

This is due to the variable "annotations" is not declared in this scope. Having this declared with let/var would fix this issue.

let annotations = Locs(name: "Sultan Hotel", coord: CLLocationCoordinate2D(latitude: 5.55739, longitude: 95.3208), alarm: 0, state: 0, radius: 1)

I hope the variable "state" is also declared in somewhere in this view.

if (state == 1 ) { Text("ON ")}//used 4 times in this snippet.

Now coming to the part of passing the data to other view, if it is the single object, it can be passed while triggering the MapView(), which would be something like

//In MapView, create a variable of Locs type 
    struct MapView(){
        let annotation: Locs //Use this variable to pin on the Map.
        //Rest of the code for View.
    }

//In Navigation View, at NavigationLink pass Locs object as argument
NavigationLink(destination: MapView(annotation: Locs(name: "Sultan Hotel", coord: CLLocationCoordinate2D(latitude: 5.55739, longitude: 95.3208), alarm: 0, state: 0, radius: 1))){

If it is going to be handling map annotations to and fro, i would strongly recommend declaring annotations object as ObservableObject and use it as ObservedObject at MapView