从lat/long数组注释地图在展开时出错(swift)

从lat/long数组注释地图在展开时出错(swift),swift,firebase,firebase-realtime-database,mkannotationview,Swift,Firebase,Firebase Realtime Database,Mkannotationview,我已经连续搜索了两天,我被这个错误困住了: 致命错误:在展开可选值时意外发现nil (lldb) 当我尝试注释从Firebase提取的纬度/经度数据数组时,就会发生这种情况 我能够成功地使用相同的数据在UITable视图中显示,但问题是当我尝试将该数据注释到地图时 目标:一次多个注释。要让存储在Firebase中的每个用户都能在地图上用任何lat/long Firebase为其添加的注释 我读到可能我还没有初始化地图视图。但是我能够成功地添加一个注释 class ViewController:

我已经连续搜索了两天,我被这个错误困住了:

致命错误:在展开可选值时意外发现nil (lldb)

当我尝试注释从Firebase提取的纬度/经度数据数组时,就会发生这种情况

我能够成功地使用相同的数据在UITable视图中显示,但问题是当我尝试将该数据注释到地图时

目标:一次多个注释。要让存储在Firebase中的每个用户都能在地图上用任何lat/long Firebase为其添加的注释

我读到可能我还没有初始化地图视图。但是我能够成功地添加一个注释

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

var mapView: MKMapView!

var userPinView: MKAnnotationView?
var locationManager: CLLocationManager = CLLocationManager()
var startLocation: CLLocationManager!
var latitude: String?
var longitude: String?
var loc: String?

let cellId = "cellId"
let pinId = "pinId"
var users = [User]()


override func viewDidLoad() {
    super.viewDidLoad()



    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handlelogout))
    navigationItem.leftBarButtonItem?.tintColor = UIColor.purple


    //navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Cast", style: .plain, target: self, action: #selector( handleStoreUserLocation))

    var rightCastBarButtonItem: UIBarButtonItem = UIBarButtonItem(title: "Cast", style: .plain, target: self, action: #selector( handleStoreUserLocation))
    var rightWhoIsCastingListBarButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.search, target: self, action: #selector(ViewController.castListTapped))
    self.navigationItem.setRightBarButtonItems([rightCastBarButtonItem, rightWhoIsCastingListBarButton], animated: true)
    navigationItem.rightBarButtonItem?.tintColor = UIColor.purple


    checkIfUserIsLoggedIn()
    fetchAllBroadcasts()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
    startLocation = nil



    }


//this function will grab the current users location and display it as long and lat numbers. Firebase/GEOFire will then need to reference these coordinates when broadcasting location to other users.  
func locationManager(_ manager: CLLocationManager,
                     didUpdateLocations locations: [CLLocation])
{
    let latestLocation: CLLocation = locations[locations.count - 1]

    latitude = String(format: "%.4f",
                           latestLocation.coordinate.latitude)
    longitude = String(format: "%.4f",
                            latestLocation.coordinate.longitude)

   // print(latitude, longitude)

}





    //this grabs the logged in user Name and displays it in the center nav bar on main screen.
    func checkIfUserIsLoggedIn() {
        if FIRAuth.auth()?.currentUser?.uid == nil {
            perform(#selector(handlelogout), with: nil, afterDelay: 0)
        } else {
            let uid = FIRAuth.auth()?.currentUser?.uid
        FIRDatabase.database().reference().child("users").child(uid!).observe(.value, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {
                self.navigationItem.title = dictionary ["name"] as? String
            }


        }, withCancel: nil)
        }

    //adtional set up after load for MAP


    var mapView = MKMapView()

    //set map and grab user location
    mapView.showsUserLocation = true
       // mapView.showAnnotations([MKAnnotation], animated: true)
    mapView.mapType = .standard
    mapView.frame = view.frame
    mapView.tintColor = UIColor.purple
    mapView.delegate = self
    view.addSubview(mapView)



    //******Defalut Map Location********
    var location = CLLocationCoordinate2D(
        latitude: 49.2810,
        longitude: -123.0733
    )
    //tell the map what the area spanned by the region is
    var span = MKCoordinateSpanMake(0.2, 0.2)
    //define the region
    var region = MKCoordinateRegion(center: location, span: span)
    //set region
    mapView.setRegion(region, animated: true)


   // annotations for the map. this is based of the dedault location above.
   // var annotation = MKPointAnnotation()
    //annotation.coordinate = location
    //annotation.title = "Gayge HQ"
    //annotation.subtitle = "oh, hello!"
    //mapView.addAnnotation(annotation)






}

//temporary "logout" function . this will later be hidden in slide out menu
func handlelogout (){
    do{
   try FIRAuth.auth()?.signOut()
    } catch let logOutError {
        print(logOutError)
    }

    let loginController = LoginController()
    //call the constant loginController that will call the loginController.swift file
    present(loginController, animated: true, completion: nil)
}

func handleRightSlideMenu (){

}

func setUpNavBarWithUser (){

}
//*****NEEDS WORK TO FECTH ALL USER LOCATIONS FROM DICTIONARY AND THEN ANNOTATE EACH USER ON THE MAP******
func fetchAllBroadcasts() {
    FIRDatabase.database().reference().child("users").observe(.childAdded, with: {(snapshot) in


        if let dictionary = snapshot.value as? [String: AnyObject?] {
            let user = User()
            user.setValuesForKeys(dictionary)
            self.users.append(user)


            //this will crash because of background thread so lets use dispatch_async to fix
            DispatchQueue.main.async(){
                self.mapView
            }

            var locationArray: [CLLocationCoordinate2D] = []
            var longDouble = CLLocationDegrees(user.long!)
            var latDouble = CLLocationDegrees(user.lat!)

           var userBroadcastLocations = CLLocationCoordinate2D(
            latitude:  (latDouble)!, longitude:  (longDouble)!)  //array of user long and lat

            locationArray.append(userBroadcastLocations)




                self.userPinView?.annotation
                locationArray.append(userBroadcastLocations)
                var annotation = MKPointAnnotation()
                annotation.coordinate = userBroadcastLocations
                annotation.title = "title"
                annotation.subtitle = "testing"
            //self.mapView.addAnnotations([locationArray.MKPinAnnotation])
            self.mapView.showAnnotations([locationArray as! MKAnnotation], animated: true)
            }

    }, withCancel: nil)

}




//launch a list view of all users broadcasting via seperate viewController (BroadcastListController)
func castListTapped(send: UIButton){
    let broadcastListController = BroadcastListController()
    let navController = UINavigationController(rootViewController: broadcastListController)
    present(navController, animated: true, completion: nil)


    }


func handleStoreUserLocation(){

    let uid = FIRAuth.auth()?.currentUser?.uid
    let ref = FIRDatabase.database().reference().child("users").child(uid!)
    let childRef = ref.childByAutoId()


    if latitude == nil && longitude == nil {
        let values = ["lat": 0, "long": 0]
        ref.updateChildValues(values)
    }else{
    let values = ["lat": latitude, "long": longitude]
    ref.updateChildValues(values)
    }
}
}


老实说,我可能已经看过答案了,问题是答案太高级了,我不知道什么适用于我的代码。因此,这就是我寻求支持的原因。谢谢你所能做的一切

它在哪条线上断?你能设置异常断点并在发生异常的地方发布吗?你有很多
在此代码中。每当你把一个
在某些事情之后,你是说它不可能是零。如果是,则应用程序会因该警告而崩溃。我假设它发生在这里,如果它发生在您执行firebase工作时:
var longDouble=CLLocationDegrees(user.long!)
它在哪一行中断?你能设置异常断点并在发生异常的地方发布吗?你有很多
在此代码中。每当你把一个
在某些事情之后,你是说它不可能是零。如果是,则应用程序会因该警告而崩溃。如果它发生在您执行firebase工作时,我会假设它就发生在这里:
var longDouble=CLLocationDegrees(user.long!)