I have my homeview with some tab bar buttons which lead to different views, but when I try to use it when i get into my ContentView which is a map, I get this error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value which points to
NavigationLink(destination: mapView(name: self.name,
geopoints: self.obs.data["data"]
Any ideas how can i fix it?
This is part of my homeView
struct HomeView: View {
@ObservedObject var authenticationViewModel: AuthenticationViewModel
@StateObject var linkViewModel: LinkViewModel = LinkViewModel()
var body: some View {
NavigationView {
TabView{
VStack {
Text("Welcome back, \(authenticationViewModel.user?.email ?? "No user")")
.padding(.top, 32)
Spacer()
LinkView(linkViewModel: linkViewModel)
}
.tabItem {
Label("Home", systemImage: "house.fill")
}
ContentView()
.tabItem {
Label("Car Location", systemImage: "car.fill")
}
ProfileView(authenticationViewModel: authenticationViewModel)
.tabItem {
Label("Profile", systemImage: "person.fill")
}
}
.navigationBarTitleDisplayMode(.inline)
.navigationTitle("Home")
.toolbar {
Button("Logout") {
authenticationViewModel.logout()
}
}
}
}
}
And this is my ContentView
import SwiftUI
import Firebase
import FirebaseFirestore
import CoreLocation
import MapKit
struct ContentView: View {
@State var name = ""
@ObservedObject var obs = observer()
var body: some View {
NavigationView{
VStack{
TextField("Enter Name", text:
$name).textFieldStyle(RoundedBorderTextFieldStyle())
if name != ""{
NavigationLink(destination: mapView(name: self.name,
geopoints: self.obs.data["data"] as! [String: GeoPoint])
.navigationBarTitle("", displayMode: .inline)) {
Text("Share Location")
}
}
}.padding()
.navigationBarTitle("Location sharing")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct mapView : UIViewRepresentable {
var name = ""
var geopoints : [String : GeoPoint]
func makeCoordinator() -> mapView.Coordinator {
return mapView.Coordinator(parent1: self)
}
let map = MKMapView()
let manager = CLLocationManager()
func makeUIView(context: UIViewRepresentableContext<mapView>) -> MKMapView {
manager.delegate = context.coordinator
manager.startUpdatingLocation()
map.showsUserLocation = true
let center = CLLocationCoordinate2D(latitude: 19.53581037522986,
longitude: -99.22177511718445)
let region = MKCoordinateRegion(center: center, latitudinalMeters: 1000,
longitudinalMeters: 1000)
map.region = region
manager.requestWhenInUseAuthorization()
return map
}
func updateUIView(_ uiView: MKMapView, context: UIViewRepresentableContext<mapView>) {
for i in geopoints{
if i.key != name{
let point = MKPointAnnotation()
point.coordinate = CLLocationCoordinate2D(latitude: i.value.latitude,
longitude: i.value.longitude)
point.title = i.key
uiView.removeAnnotations(uiView.annotations)
uiView.addAnnotation(point)
}
}
}
class Coordinator : NSObject, CLLocationManagerDelegate{
var parent: mapView
init(parent1 : mapView){
parent = parent1
}
func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) {
if status == .denied{
print("Denied")
}
if status == .authorizedWhenInUse{
print("Authorized")
}
}
func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
let last = locations.last
let db = Firestore.firestore()
db.collection("locations").document("qdEq1Pixu0GHHZjVT1MV")
.setData(["updates" : [self.parent.name : GeoPoint(latitude: (last?.coordinate.latitude)!,
longitude: (last?.coordinate.longitude)!)]],
merge: true) {
(err) in
if err != nil{
print((err?.localizedDescription)!)
return
}
print("Sucess")
}
}
}
}
class observer : ObservableObject{
@Published var data = [String : Any]()
init() {
let db = Firestore.firestore()
db.collection("locations").document("qdEq1Pixu0GHHZjVT1MV").addSnapshotListener { (snap, err)
in
if err != nil{
print((err?.localizedDescription)!)
return
}
let updates = snap?.get("updates") as! [String : GeoPoint]
self.data["data"] = updates
}
}
}
I tried changing variable declarations but didn't worked. Any help is appreciated.