Set two var‘s after each other

Hi guys! I am a beginner in SwiftUI. I have a problem in my code and could not find an Internet article about it. Here is the full code:


import SwiftUI

import MapKit



struct Place: Identifiable {

    let id = UUID()

    let name: String

    let latitude: Double

    let longitude: Double

    var coordinate: CLLocationCoordinate2D {

        CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

    }

}



struct ContentView: View {

    @StateObject private var viewModel = ContentViewModel()

    @State private var showingPopover = false

    @State private var selectedPlaceName = "ERROR"

    let places = [

        Place(name: "…", latitude: 0.0, longitude: 0.0)

    ]

    

    @State var region = MKCoordinateRegion(

        center: CLLocationCoordinate2D(

            latitude: 48.71424,

            longitude: 8.741835

        ),

        span: MKCoordinateSpan(

            latitudeDelta: 0.01    ,

            longitudeDelta: 0.01

        )

    )

    

    

    var body: some View {


        Map(

            coordinateRegion: $region,

            showsUserLocation: true,

            annotationItems: places) { place in

                MapAnnotation(coordinate: place.coordinate) {

                    Image(systemName: "pin.fill")

                        .foregroundColor(Color.red)

                        .onTapGesture(count: 1, perform: {

                            selectedPlaceName = place.name;

                            showingPopover = true

                        }

                        )

                }

                }

            

            .accentColor(Color(.systemBlue))

            .statusBar(hidden: true)

            .onAppear{

                viewModel.checkIfLocationServicesIsEnabled()

            }

            .popover(isPresented: $showingPopover){

                Text("Description:" + selectedPlaceName)

            }

    }

    final class ContentViewModel: NSObject, ObservableObject, CLLocationManagerDelegate {

        @Published var region = MKCoordinateRegion(

            center: CLLocationCoordinate2D(

                latitude: 0.0,

                longitude: 0.0

            ),

            span: MKCoordinateSpan(

                latitudeDelta: 0.01    ,

                longitudeDelta: 0.01

            )

        )

        var locationManager: CLLocationManager?

        

        func checkIfLocationServicesIsEnabled() {

            if CLLocationManager.locationServicesEnabled() {

                locationManager = CLLocationManager()

                locationManager?.delegate = self

            } else {

                print("Please turn on location in the settings.")

            }

        }

        private func checkLocationAuthorization() {

            guard let locationManager = locationManager else { return }

            switch locationManager.authorizationStatus {

            case .notDetermined:

                locationManager.requestWhenInUseAuthorization()

            case .restricted:

                print("Please activate location services in the settings.")

            case .denied:

                print("You have deactivated location services, please activate it in the installations.")

            case .authorizedAlways, .authorizedWhenInUse:

                region = MKCoordinateRegion(center: locationManager.location!.coordinate, span: MKCoordinateSpan(

                    latitudeDelta: 0.1,

                    longitudeDelta: 0.1

                ))

            @unknown default:

                break

            } 

        }

        func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {

            checkLocationAuthorization()

        }

    }

}

This is the part that causes me difficulties:

annotationItems: places) { place in

                MapAnnotation(coordinate: place.coordinate) {

                    Image(systemName: "pin.fill")

                        .foregroundColor(Color.red)

                        .onTapGesture(count: 1, perform: {

                            selectedPlaceName = place.name;

                            showingPopover = true

                        }

I want to set the variable „selectedPlaceName“ before setting „showingPopover“

I don’t know how to proceed. Can you help me? Thanks!

I want to set the variable „selectedPlaceName“ before setting „showingPopover“ > Can you explain what you want to achieve? If the order of setting the variables is the purpose of you, you are doing it as you describe. If your actual purpose is sort of using selectedPlaceName in the popover, you may need to show more code.

The one variable (showingPopover) shows a popover. The other variable (selectedPlaceName) sets the value for the contents of the popover. The placeholder for selectedPlaceName is ERROR (if it doesn‘t work, this text will be displayed). So I would now have to set selectedPlaceName before showingPopover (so that it works correctly). What am I doing wrong?

Seems you need to show more code. What am I doing wrong? One thing clearly wrong is that you are not showing enough code.

That’s my whole Code, how should I show more?

Sorry, I have been missing the part where you use popover using selectedPlaceName.

So you can’t help me with my problem?

Set two var‘s after each other
 
 
Q