I get this error when i run my app. Thread 1: Fatal error: Unexpectedly found nil while unwrapping an...





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Whenever I run my app, it always crashes anywhere it states currentUserID. Not sure why.
I printed the the currentUserID value and its nil.
How can I correct it so it runs my app well. I can share the rest of the code if needed.



import UIKit
import MapKit
import CoreLocation
import Firebase
//import FirebaseDatabase

enum AnnotationType {
case pickup
case destination
case driver
}

enum ButtonAction {
case requestRide
case getDirectionsToPassenger
case getDirectionToDestination
case startTrip
case endTrip
}

class HomeVC: UIViewController, Alertable {

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var centerMapBtn: UIButton!
@IBOutlet weak var destinationTextField: UITextField!
@IBOutlet weak var destinationCircle: UIView!
@IBOutlet weak var cancelBtn: UIButton!
@IBOutlet weak var actionBtn: UIButton!




var delegate: CenterVCDelegate?

var manager: CLLocationManager?

let currentUserId = Auth.auth().currentUser?.uid
// "IRD70YtgEyWRpaH8LkkjHZmjXPo1"


var ref: DatabaseReference!

var regionRadius: CLLocationDistance = 1000

var tableView = UITableView()

var matchingItems: [MKMapItem] = [MKMapItem]()

var route: MKRoute!

var selectedItemPlacemark: MKPlacemark? = nil

var actionForButton: ButtonAction = .requestRide

override func viewDidLoad() {
super.viewDidLoad()



ref = Database.database().reference()
// print(ref.child("users").value(forKey: "users"))
self.ref.child("userse").childByAutoId().setValue("wise")

self.ref.child("users").child("sdsd").setValue("data")


// print(Auth.auth().currentUser?.uid)
// print(currentUserId)


manager = CLLocationManager()
manager?.delegate = self
manager?.desiredAccuracy = kCLLocationAccuracyBest

checkLocationAuthStatus()

mapView.delegate = self
destinationTextField.delegate = self

centerMapOnUserLocation()

DataService.instance.REF_DRIVERS.observe(.value, with: { (snapshot) in
self.loadDriverAnnotationFromFB()



DataService.instance.passengerIsOnTrip(passengerKey: self.currentUserId!, handler: { (isOnTrip, driverKey, tripKey) in
if isOnTrip == true {
self.zoom(toFitAnnotationsFromMapView: self.mapView, forActiveTripWithDriver: true, withKey: driverKey)
}
})
})

cancelBtn.alpha = 0.0
UpdateService.instance.observeTrips { (tripDict) in
if let tripDict = tripDict {
let pickupCoordinateArray = tripDict["pickupCoordinate"] as! NSArray
let tripKey = tripDict["passengerKey"] as! String
let acceptanceStatus = tripDict["tripIsAccepted"] as! Bool

if acceptanceStatus == false {
//Broadcast to all driver
DataService.instance.driverIsAvailable(key: self.currentUserId!, handler: { (available) in
//check for errors
if let available = available {
if available == true {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let pickupVC = storyboard.instantiateViewController(withIdentifier: "PickupVC") as? PickupVC
pickupVC?.initData(coordinate: CLLocationCoordinate2D(latitude: pickupCoordinateArray[0] as! CLLocationDegrees, longitude: pickupCoordinateArray[1] as! CLLocationDegrees), passengerKey: tripKey)
self.present(pickupVC!, animated: true, completion: nil)
}
}

})
}
}
}

}



override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if self.currentUserId == nil {
print("login")
}
DataService.instance.userIsDriver(userKey: self.currentUserId!, handler: { (status) in
if status == true {
self.buttonsForDriver(areHidden: true)
}
})

DataService.instance.REF_TRIPS.observe(.childRemoved, with: { (removedTripSnapshot) in
let removedTripDict = removedTripSnapshot.value as? [String: AnyObject]
if removedTripDict?["driverKey"] != nil {
DataService.instance.REF_DRIVERS.child(removedTripDict?["driverKey"] as! String).updateChildValues(["driverIsOnTrip": false])
}
DataService.instance.userIsDriver(userKey: self.currentUserId!, handler: { (isDriver) in
if isDriver == true {
// Remove Overlays and annotation and hide request ride and cancel button
self.removeOverlaysAndAnnotations(forDrivers: false, forPassengers: true)
} else {
self.cancelBtn.fadeTo(alphaValue: 0.0, withDuration: 0.2)
self.destinationTextField.isUserInteractionEnabled = true
self.destinationTextField.text = ""

// Remove all map annotation and overlays
self.removeOverlaysAndAnnotations(forDrivers: false, forPassengers: true)
self.centerMapOnUserLocation()
}
})
})
DataService.instance.driverIsOnTrip(driverkey: self.currentUserId!, handler: { (isOnTrip, driverKey, tripKey) in
if isOnTrip == true {
DataService.instance.REF_TRIPS.observeSingleEvent(of: .value, with: { (tripSnapshot) in
if let tripSnapshot = tripSnapshot.children.allObjects as? [DataSnapshot] {
for trip in tripSnapshot {
if trip.childSnapshot(forPath: "driverKey").value as? String == self.currentUserId! {
let pickupCoordinateArray = trip.childSnapshot(forPath: "pickupCoordinate").value as! NSArray
let pickupCoordinate = CLLocationCoordinate2D(latitude: pickupCoordinateArray[0] as! CLLocationDegrees, longitude: pickupCoordinateArray[1] as! CLLocationDegrees)
let pickupPlacemark = MKPlacemark(coordinate: pickupCoordinate)
self.dropPinFor(placemark: pickupPlacemark)
self.searchMapKitForResultsWithPolyline(forOriginMapItem: nil, withDestinationMapItem: MKMapItem(placemark: pickupPlacemark))
self.setCustomRegion(forAnnotationType: .pickup, withCoordinate: pickupCoordinate)

self.actionForButton = .getDirectionsToPassenger
self.actionBtn.setTitle("GET DIRECTION", for: .normal)

// Fade in the action button
self.buttonsForDriver(areHidden: false)

}
}
}
})









share|improve this question

























  • for all the forced(!) variables you have something is nil. Review DatabaseReference for example.

    – Alex Bailey
    Nov 23 '18 at 4:39











  • If you place a breakpoint at the start of viewDidLoad, what is the value of "currentUserId"?

    – David Rees
    Nov 23 '18 at 4:52











  • when you call auth may be chances HomeVC is called before authentication and you got currentUserId nil

    – Jatin Kathrotiya
    Nov 23 '18 at 4:57











  • Use Exception Breakpoint and you will come to which variable getting nil value

    – Manish Mahajan
    Nov 23 '18 at 5:30











  • Possible duplicate of What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?

    – Tamás Sengel
    Nov 23 '18 at 22:41


















0















Whenever I run my app, it always crashes anywhere it states currentUserID. Not sure why.
I printed the the currentUserID value and its nil.
How can I correct it so it runs my app well. I can share the rest of the code if needed.



import UIKit
import MapKit
import CoreLocation
import Firebase
//import FirebaseDatabase

enum AnnotationType {
case pickup
case destination
case driver
}

enum ButtonAction {
case requestRide
case getDirectionsToPassenger
case getDirectionToDestination
case startTrip
case endTrip
}

class HomeVC: UIViewController, Alertable {

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var centerMapBtn: UIButton!
@IBOutlet weak var destinationTextField: UITextField!
@IBOutlet weak var destinationCircle: UIView!
@IBOutlet weak var cancelBtn: UIButton!
@IBOutlet weak var actionBtn: UIButton!




var delegate: CenterVCDelegate?

var manager: CLLocationManager?

let currentUserId = Auth.auth().currentUser?.uid
// "IRD70YtgEyWRpaH8LkkjHZmjXPo1"


var ref: DatabaseReference!

var regionRadius: CLLocationDistance = 1000

var tableView = UITableView()

var matchingItems: [MKMapItem] = [MKMapItem]()

var route: MKRoute!

var selectedItemPlacemark: MKPlacemark? = nil

var actionForButton: ButtonAction = .requestRide

override func viewDidLoad() {
super.viewDidLoad()



ref = Database.database().reference()
// print(ref.child("users").value(forKey: "users"))
self.ref.child("userse").childByAutoId().setValue("wise")

self.ref.child("users").child("sdsd").setValue("data")


// print(Auth.auth().currentUser?.uid)
// print(currentUserId)


manager = CLLocationManager()
manager?.delegate = self
manager?.desiredAccuracy = kCLLocationAccuracyBest

checkLocationAuthStatus()

mapView.delegate = self
destinationTextField.delegate = self

centerMapOnUserLocation()

DataService.instance.REF_DRIVERS.observe(.value, with: { (snapshot) in
self.loadDriverAnnotationFromFB()



DataService.instance.passengerIsOnTrip(passengerKey: self.currentUserId!, handler: { (isOnTrip, driverKey, tripKey) in
if isOnTrip == true {
self.zoom(toFitAnnotationsFromMapView: self.mapView, forActiveTripWithDriver: true, withKey: driverKey)
}
})
})

cancelBtn.alpha = 0.0
UpdateService.instance.observeTrips { (tripDict) in
if let tripDict = tripDict {
let pickupCoordinateArray = tripDict["pickupCoordinate"] as! NSArray
let tripKey = tripDict["passengerKey"] as! String
let acceptanceStatus = tripDict["tripIsAccepted"] as! Bool

if acceptanceStatus == false {
//Broadcast to all driver
DataService.instance.driverIsAvailable(key: self.currentUserId!, handler: { (available) in
//check for errors
if let available = available {
if available == true {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let pickupVC = storyboard.instantiateViewController(withIdentifier: "PickupVC") as? PickupVC
pickupVC?.initData(coordinate: CLLocationCoordinate2D(latitude: pickupCoordinateArray[0] as! CLLocationDegrees, longitude: pickupCoordinateArray[1] as! CLLocationDegrees), passengerKey: tripKey)
self.present(pickupVC!, animated: true, completion: nil)
}
}

})
}
}
}

}



override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if self.currentUserId == nil {
print("login")
}
DataService.instance.userIsDriver(userKey: self.currentUserId!, handler: { (status) in
if status == true {
self.buttonsForDriver(areHidden: true)
}
})

DataService.instance.REF_TRIPS.observe(.childRemoved, with: { (removedTripSnapshot) in
let removedTripDict = removedTripSnapshot.value as? [String: AnyObject]
if removedTripDict?["driverKey"] != nil {
DataService.instance.REF_DRIVERS.child(removedTripDict?["driverKey"] as! String).updateChildValues(["driverIsOnTrip": false])
}
DataService.instance.userIsDriver(userKey: self.currentUserId!, handler: { (isDriver) in
if isDriver == true {
// Remove Overlays and annotation and hide request ride and cancel button
self.removeOverlaysAndAnnotations(forDrivers: false, forPassengers: true)
} else {
self.cancelBtn.fadeTo(alphaValue: 0.0, withDuration: 0.2)
self.destinationTextField.isUserInteractionEnabled = true
self.destinationTextField.text = ""

// Remove all map annotation and overlays
self.removeOverlaysAndAnnotations(forDrivers: false, forPassengers: true)
self.centerMapOnUserLocation()
}
})
})
DataService.instance.driverIsOnTrip(driverkey: self.currentUserId!, handler: { (isOnTrip, driverKey, tripKey) in
if isOnTrip == true {
DataService.instance.REF_TRIPS.observeSingleEvent(of: .value, with: { (tripSnapshot) in
if let tripSnapshot = tripSnapshot.children.allObjects as? [DataSnapshot] {
for trip in tripSnapshot {
if trip.childSnapshot(forPath: "driverKey").value as? String == self.currentUserId! {
let pickupCoordinateArray = trip.childSnapshot(forPath: "pickupCoordinate").value as! NSArray
let pickupCoordinate = CLLocationCoordinate2D(latitude: pickupCoordinateArray[0] as! CLLocationDegrees, longitude: pickupCoordinateArray[1] as! CLLocationDegrees)
let pickupPlacemark = MKPlacemark(coordinate: pickupCoordinate)
self.dropPinFor(placemark: pickupPlacemark)
self.searchMapKitForResultsWithPolyline(forOriginMapItem: nil, withDestinationMapItem: MKMapItem(placemark: pickupPlacemark))
self.setCustomRegion(forAnnotationType: .pickup, withCoordinate: pickupCoordinate)

self.actionForButton = .getDirectionsToPassenger
self.actionBtn.setTitle("GET DIRECTION", for: .normal)

// Fade in the action button
self.buttonsForDriver(areHidden: false)

}
}
}
})









share|improve this question

























  • for all the forced(!) variables you have something is nil. Review DatabaseReference for example.

    – Alex Bailey
    Nov 23 '18 at 4:39











  • If you place a breakpoint at the start of viewDidLoad, what is the value of "currentUserId"?

    – David Rees
    Nov 23 '18 at 4:52











  • when you call auth may be chances HomeVC is called before authentication and you got currentUserId nil

    – Jatin Kathrotiya
    Nov 23 '18 at 4:57











  • Use Exception Breakpoint and you will come to which variable getting nil value

    – Manish Mahajan
    Nov 23 '18 at 5:30











  • Possible duplicate of What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?

    – Tamás Sengel
    Nov 23 '18 at 22:41














0












0








0








Whenever I run my app, it always crashes anywhere it states currentUserID. Not sure why.
I printed the the currentUserID value and its nil.
How can I correct it so it runs my app well. I can share the rest of the code if needed.



import UIKit
import MapKit
import CoreLocation
import Firebase
//import FirebaseDatabase

enum AnnotationType {
case pickup
case destination
case driver
}

enum ButtonAction {
case requestRide
case getDirectionsToPassenger
case getDirectionToDestination
case startTrip
case endTrip
}

class HomeVC: UIViewController, Alertable {

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var centerMapBtn: UIButton!
@IBOutlet weak var destinationTextField: UITextField!
@IBOutlet weak var destinationCircle: UIView!
@IBOutlet weak var cancelBtn: UIButton!
@IBOutlet weak var actionBtn: UIButton!




var delegate: CenterVCDelegate?

var manager: CLLocationManager?

let currentUserId = Auth.auth().currentUser?.uid
// "IRD70YtgEyWRpaH8LkkjHZmjXPo1"


var ref: DatabaseReference!

var regionRadius: CLLocationDistance = 1000

var tableView = UITableView()

var matchingItems: [MKMapItem] = [MKMapItem]()

var route: MKRoute!

var selectedItemPlacemark: MKPlacemark? = nil

var actionForButton: ButtonAction = .requestRide

override func viewDidLoad() {
super.viewDidLoad()



ref = Database.database().reference()
// print(ref.child("users").value(forKey: "users"))
self.ref.child("userse").childByAutoId().setValue("wise")

self.ref.child("users").child("sdsd").setValue("data")


// print(Auth.auth().currentUser?.uid)
// print(currentUserId)


manager = CLLocationManager()
manager?.delegate = self
manager?.desiredAccuracy = kCLLocationAccuracyBest

checkLocationAuthStatus()

mapView.delegate = self
destinationTextField.delegate = self

centerMapOnUserLocation()

DataService.instance.REF_DRIVERS.observe(.value, with: { (snapshot) in
self.loadDriverAnnotationFromFB()



DataService.instance.passengerIsOnTrip(passengerKey: self.currentUserId!, handler: { (isOnTrip, driverKey, tripKey) in
if isOnTrip == true {
self.zoom(toFitAnnotationsFromMapView: self.mapView, forActiveTripWithDriver: true, withKey: driverKey)
}
})
})

cancelBtn.alpha = 0.0
UpdateService.instance.observeTrips { (tripDict) in
if let tripDict = tripDict {
let pickupCoordinateArray = tripDict["pickupCoordinate"] as! NSArray
let tripKey = tripDict["passengerKey"] as! String
let acceptanceStatus = tripDict["tripIsAccepted"] as! Bool

if acceptanceStatus == false {
//Broadcast to all driver
DataService.instance.driverIsAvailable(key: self.currentUserId!, handler: { (available) in
//check for errors
if let available = available {
if available == true {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let pickupVC = storyboard.instantiateViewController(withIdentifier: "PickupVC") as? PickupVC
pickupVC?.initData(coordinate: CLLocationCoordinate2D(latitude: pickupCoordinateArray[0] as! CLLocationDegrees, longitude: pickupCoordinateArray[1] as! CLLocationDegrees), passengerKey: tripKey)
self.present(pickupVC!, animated: true, completion: nil)
}
}

})
}
}
}

}



override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if self.currentUserId == nil {
print("login")
}
DataService.instance.userIsDriver(userKey: self.currentUserId!, handler: { (status) in
if status == true {
self.buttonsForDriver(areHidden: true)
}
})

DataService.instance.REF_TRIPS.observe(.childRemoved, with: { (removedTripSnapshot) in
let removedTripDict = removedTripSnapshot.value as? [String: AnyObject]
if removedTripDict?["driverKey"] != nil {
DataService.instance.REF_DRIVERS.child(removedTripDict?["driverKey"] as! String).updateChildValues(["driverIsOnTrip": false])
}
DataService.instance.userIsDriver(userKey: self.currentUserId!, handler: { (isDriver) in
if isDriver == true {
// Remove Overlays and annotation and hide request ride and cancel button
self.removeOverlaysAndAnnotations(forDrivers: false, forPassengers: true)
} else {
self.cancelBtn.fadeTo(alphaValue: 0.0, withDuration: 0.2)
self.destinationTextField.isUserInteractionEnabled = true
self.destinationTextField.text = ""

// Remove all map annotation and overlays
self.removeOverlaysAndAnnotations(forDrivers: false, forPassengers: true)
self.centerMapOnUserLocation()
}
})
})
DataService.instance.driverIsOnTrip(driverkey: self.currentUserId!, handler: { (isOnTrip, driverKey, tripKey) in
if isOnTrip == true {
DataService.instance.REF_TRIPS.observeSingleEvent(of: .value, with: { (tripSnapshot) in
if let tripSnapshot = tripSnapshot.children.allObjects as? [DataSnapshot] {
for trip in tripSnapshot {
if trip.childSnapshot(forPath: "driverKey").value as? String == self.currentUserId! {
let pickupCoordinateArray = trip.childSnapshot(forPath: "pickupCoordinate").value as! NSArray
let pickupCoordinate = CLLocationCoordinate2D(latitude: pickupCoordinateArray[0] as! CLLocationDegrees, longitude: pickupCoordinateArray[1] as! CLLocationDegrees)
let pickupPlacemark = MKPlacemark(coordinate: pickupCoordinate)
self.dropPinFor(placemark: pickupPlacemark)
self.searchMapKitForResultsWithPolyline(forOriginMapItem: nil, withDestinationMapItem: MKMapItem(placemark: pickupPlacemark))
self.setCustomRegion(forAnnotationType: .pickup, withCoordinate: pickupCoordinate)

self.actionForButton = .getDirectionsToPassenger
self.actionBtn.setTitle("GET DIRECTION", for: .normal)

// Fade in the action button
self.buttonsForDriver(areHidden: false)

}
}
}
})









share|improve this question
















Whenever I run my app, it always crashes anywhere it states currentUserID. Not sure why.
I printed the the currentUserID value and its nil.
How can I correct it so it runs my app well. I can share the rest of the code if needed.



import UIKit
import MapKit
import CoreLocation
import Firebase
//import FirebaseDatabase

enum AnnotationType {
case pickup
case destination
case driver
}

enum ButtonAction {
case requestRide
case getDirectionsToPassenger
case getDirectionToDestination
case startTrip
case endTrip
}

class HomeVC: UIViewController, Alertable {

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var centerMapBtn: UIButton!
@IBOutlet weak var destinationTextField: UITextField!
@IBOutlet weak var destinationCircle: UIView!
@IBOutlet weak var cancelBtn: UIButton!
@IBOutlet weak var actionBtn: UIButton!




var delegate: CenterVCDelegate?

var manager: CLLocationManager?

let currentUserId = Auth.auth().currentUser?.uid
// "IRD70YtgEyWRpaH8LkkjHZmjXPo1"


var ref: DatabaseReference!

var regionRadius: CLLocationDistance = 1000

var tableView = UITableView()

var matchingItems: [MKMapItem] = [MKMapItem]()

var route: MKRoute!

var selectedItemPlacemark: MKPlacemark? = nil

var actionForButton: ButtonAction = .requestRide

override func viewDidLoad() {
super.viewDidLoad()



ref = Database.database().reference()
// print(ref.child("users").value(forKey: "users"))
self.ref.child("userse").childByAutoId().setValue("wise")

self.ref.child("users").child("sdsd").setValue("data")


// print(Auth.auth().currentUser?.uid)
// print(currentUserId)


manager = CLLocationManager()
manager?.delegate = self
manager?.desiredAccuracy = kCLLocationAccuracyBest

checkLocationAuthStatus()

mapView.delegate = self
destinationTextField.delegate = self

centerMapOnUserLocation()

DataService.instance.REF_DRIVERS.observe(.value, with: { (snapshot) in
self.loadDriverAnnotationFromFB()



DataService.instance.passengerIsOnTrip(passengerKey: self.currentUserId!, handler: { (isOnTrip, driverKey, tripKey) in
if isOnTrip == true {
self.zoom(toFitAnnotationsFromMapView: self.mapView, forActiveTripWithDriver: true, withKey: driverKey)
}
})
})

cancelBtn.alpha = 0.0
UpdateService.instance.observeTrips { (tripDict) in
if let tripDict = tripDict {
let pickupCoordinateArray = tripDict["pickupCoordinate"] as! NSArray
let tripKey = tripDict["passengerKey"] as! String
let acceptanceStatus = tripDict["tripIsAccepted"] as! Bool

if acceptanceStatus == false {
//Broadcast to all driver
DataService.instance.driverIsAvailable(key: self.currentUserId!, handler: { (available) in
//check for errors
if let available = available {
if available == true {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let pickupVC = storyboard.instantiateViewController(withIdentifier: "PickupVC") as? PickupVC
pickupVC?.initData(coordinate: CLLocationCoordinate2D(latitude: pickupCoordinateArray[0] as! CLLocationDegrees, longitude: pickupCoordinateArray[1] as! CLLocationDegrees), passengerKey: tripKey)
self.present(pickupVC!, animated: true, completion: nil)
}
}

})
}
}
}

}



override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if self.currentUserId == nil {
print("login")
}
DataService.instance.userIsDriver(userKey: self.currentUserId!, handler: { (status) in
if status == true {
self.buttonsForDriver(areHidden: true)
}
})

DataService.instance.REF_TRIPS.observe(.childRemoved, with: { (removedTripSnapshot) in
let removedTripDict = removedTripSnapshot.value as? [String: AnyObject]
if removedTripDict?["driverKey"] != nil {
DataService.instance.REF_DRIVERS.child(removedTripDict?["driverKey"] as! String).updateChildValues(["driverIsOnTrip": false])
}
DataService.instance.userIsDriver(userKey: self.currentUserId!, handler: { (isDriver) in
if isDriver == true {
// Remove Overlays and annotation and hide request ride and cancel button
self.removeOverlaysAndAnnotations(forDrivers: false, forPassengers: true)
} else {
self.cancelBtn.fadeTo(alphaValue: 0.0, withDuration: 0.2)
self.destinationTextField.isUserInteractionEnabled = true
self.destinationTextField.text = ""

// Remove all map annotation and overlays
self.removeOverlaysAndAnnotations(forDrivers: false, forPassengers: true)
self.centerMapOnUserLocation()
}
})
})
DataService.instance.driverIsOnTrip(driverkey: self.currentUserId!, handler: { (isOnTrip, driverKey, tripKey) in
if isOnTrip == true {
DataService.instance.REF_TRIPS.observeSingleEvent(of: .value, with: { (tripSnapshot) in
if let tripSnapshot = tripSnapshot.children.allObjects as? [DataSnapshot] {
for trip in tripSnapshot {
if trip.childSnapshot(forPath: "driverKey").value as? String == self.currentUserId! {
let pickupCoordinateArray = trip.childSnapshot(forPath: "pickupCoordinate").value as! NSArray
let pickupCoordinate = CLLocationCoordinate2D(latitude: pickupCoordinateArray[0] as! CLLocationDegrees, longitude: pickupCoordinateArray[1] as! CLLocationDegrees)
let pickupPlacemark = MKPlacemark(coordinate: pickupCoordinate)
self.dropPinFor(placemark: pickupPlacemark)
self.searchMapKitForResultsWithPolyline(forOriginMapItem: nil, withDestinationMapItem: MKMapItem(placemark: pickupPlacemark))
self.setCustomRegion(forAnnotationType: .pickup, withCoordinate: pickupCoordinate)

self.actionForButton = .getDirectionsToPassenger
self.actionBtn.setTitle("GET DIRECTION", for: .normal)

// Fade in the action button
self.buttonsForDriver(areHidden: false)

}
}
}
})






ios swift firebase mobile






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 5:34









Rajesh Pandya

1,34641021




1,34641021










asked Nov 23 '18 at 4:30









Wise OsagieWise Osagie

194




194













  • for all the forced(!) variables you have something is nil. Review DatabaseReference for example.

    – Alex Bailey
    Nov 23 '18 at 4:39











  • If you place a breakpoint at the start of viewDidLoad, what is the value of "currentUserId"?

    – David Rees
    Nov 23 '18 at 4:52











  • when you call auth may be chances HomeVC is called before authentication and you got currentUserId nil

    – Jatin Kathrotiya
    Nov 23 '18 at 4:57











  • Use Exception Breakpoint and you will come to which variable getting nil value

    – Manish Mahajan
    Nov 23 '18 at 5:30











  • Possible duplicate of What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?

    – Tamás Sengel
    Nov 23 '18 at 22:41



















  • for all the forced(!) variables you have something is nil. Review DatabaseReference for example.

    – Alex Bailey
    Nov 23 '18 at 4:39











  • If you place a breakpoint at the start of viewDidLoad, what is the value of "currentUserId"?

    – David Rees
    Nov 23 '18 at 4:52











  • when you call auth may be chances HomeVC is called before authentication and you got currentUserId nil

    – Jatin Kathrotiya
    Nov 23 '18 at 4:57











  • Use Exception Breakpoint and you will come to which variable getting nil value

    – Manish Mahajan
    Nov 23 '18 at 5:30











  • Possible duplicate of What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?

    – Tamás Sengel
    Nov 23 '18 at 22:41

















for all the forced(!) variables you have something is nil. Review DatabaseReference for example.

– Alex Bailey
Nov 23 '18 at 4:39





for all the forced(!) variables you have something is nil. Review DatabaseReference for example.

– Alex Bailey
Nov 23 '18 at 4:39













If you place a breakpoint at the start of viewDidLoad, what is the value of "currentUserId"?

– David Rees
Nov 23 '18 at 4:52





If you place a breakpoint at the start of viewDidLoad, what is the value of "currentUserId"?

– David Rees
Nov 23 '18 at 4:52













when you call auth may be chances HomeVC is called before authentication and you got currentUserId nil

– Jatin Kathrotiya
Nov 23 '18 at 4:57





when you call auth may be chances HomeVC is called before authentication and you got currentUserId nil

– Jatin Kathrotiya
Nov 23 '18 at 4:57













Use Exception Breakpoint and you will come to which variable getting nil value

– Manish Mahajan
Nov 23 '18 at 5:30





Use Exception Breakpoint and you will come to which variable getting nil value

– Manish Mahajan
Nov 23 '18 at 5:30













Possible duplicate of What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?

– Tamás Sengel
Nov 23 '18 at 22:41





Possible duplicate of What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?

– Tamás Sengel
Nov 23 '18 at 22:41












1 Answer
1






active

oldest

votes


















0














There are a few issues that should be addressed and then a core issue.



The first thing is that Auth.auth().currentUser? is an optional - meaning that it could have a value or could be nil. Doing this..



let currentUserId = Auth.auth().currentUser?.uid


is telling your code that no matter if its got a value or nil, attempt to assign the .uid to currentUserId.



Protect your code by safely unwrapping options



guard let currentUser = Auth.auth().currentUser else {return}
//now you can access currentUser as it won't be nil


or



if let currentUser = Auth.auth().currentUser {
print("user is authenticated and currentUser is not nil")
} else {
print("not authenticated")
}


Now the core of the issue is, as far as I can see, you're not authenticating the user at all and that needs to happen prior to accessing Auth.auth().currentUser



Here's a complete authentication function with error handling



func authUser(user: String, pw: String) {
Auth.auth().signIn(withEmail: user, password: pw, completion: { (auth, error) in
if let x = error {
let err = x as NSError
switch err.code {
case AuthErrorCode.wrongPassword.rawValue:
print("wrong password")
case AuthErrorCode.invalidEmail.rawValue:
print("invalued email")
default:
print("unknown error")
}
} else {
if let user = auth?.user { //note; safely unwrap optional
print("uid: (user.uid)")
}
}
})
}





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440677%2fi-get-this-error-when-i-run-my-app-thread-1-fatal-error-unexpectedly-found-ni%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    There are a few issues that should be addressed and then a core issue.



    The first thing is that Auth.auth().currentUser? is an optional - meaning that it could have a value or could be nil. Doing this..



    let currentUserId = Auth.auth().currentUser?.uid


    is telling your code that no matter if its got a value or nil, attempt to assign the .uid to currentUserId.



    Protect your code by safely unwrapping options



    guard let currentUser = Auth.auth().currentUser else {return}
    //now you can access currentUser as it won't be nil


    or



    if let currentUser = Auth.auth().currentUser {
    print("user is authenticated and currentUser is not nil")
    } else {
    print("not authenticated")
    }


    Now the core of the issue is, as far as I can see, you're not authenticating the user at all and that needs to happen prior to accessing Auth.auth().currentUser



    Here's a complete authentication function with error handling



    func authUser(user: String, pw: String) {
    Auth.auth().signIn(withEmail: user, password: pw, completion: { (auth, error) in
    if let x = error {
    let err = x as NSError
    switch err.code {
    case AuthErrorCode.wrongPassword.rawValue:
    print("wrong password")
    case AuthErrorCode.invalidEmail.rawValue:
    print("invalued email")
    default:
    print("unknown error")
    }
    } else {
    if let user = auth?.user { //note; safely unwrap optional
    print("uid: (user.uid)")
    }
    }
    })
    }





    share|improve this answer




























      0














      There are a few issues that should be addressed and then a core issue.



      The first thing is that Auth.auth().currentUser? is an optional - meaning that it could have a value or could be nil. Doing this..



      let currentUserId = Auth.auth().currentUser?.uid


      is telling your code that no matter if its got a value or nil, attempt to assign the .uid to currentUserId.



      Protect your code by safely unwrapping options



      guard let currentUser = Auth.auth().currentUser else {return}
      //now you can access currentUser as it won't be nil


      or



      if let currentUser = Auth.auth().currentUser {
      print("user is authenticated and currentUser is not nil")
      } else {
      print("not authenticated")
      }


      Now the core of the issue is, as far as I can see, you're not authenticating the user at all and that needs to happen prior to accessing Auth.auth().currentUser



      Here's a complete authentication function with error handling



      func authUser(user: String, pw: String) {
      Auth.auth().signIn(withEmail: user, password: pw, completion: { (auth, error) in
      if let x = error {
      let err = x as NSError
      switch err.code {
      case AuthErrorCode.wrongPassword.rawValue:
      print("wrong password")
      case AuthErrorCode.invalidEmail.rawValue:
      print("invalued email")
      default:
      print("unknown error")
      }
      } else {
      if let user = auth?.user { //note; safely unwrap optional
      print("uid: (user.uid)")
      }
      }
      })
      }





      share|improve this answer


























        0












        0








        0







        There are a few issues that should be addressed and then a core issue.



        The first thing is that Auth.auth().currentUser? is an optional - meaning that it could have a value or could be nil. Doing this..



        let currentUserId = Auth.auth().currentUser?.uid


        is telling your code that no matter if its got a value or nil, attempt to assign the .uid to currentUserId.



        Protect your code by safely unwrapping options



        guard let currentUser = Auth.auth().currentUser else {return}
        //now you can access currentUser as it won't be nil


        or



        if let currentUser = Auth.auth().currentUser {
        print("user is authenticated and currentUser is not nil")
        } else {
        print("not authenticated")
        }


        Now the core of the issue is, as far as I can see, you're not authenticating the user at all and that needs to happen prior to accessing Auth.auth().currentUser



        Here's a complete authentication function with error handling



        func authUser(user: String, pw: String) {
        Auth.auth().signIn(withEmail: user, password: pw, completion: { (auth, error) in
        if let x = error {
        let err = x as NSError
        switch err.code {
        case AuthErrorCode.wrongPassword.rawValue:
        print("wrong password")
        case AuthErrorCode.invalidEmail.rawValue:
        print("invalued email")
        default:
        print("unknown error")
        }
        } else {
        if let user = auth?.user { //note; safely unwrap optional
        print("uid: (user.uid)")
        }
        }
        })
        }





        share|improve this answer













        There are a few issues that should be addressed and then a core issue.



        The first thing is that Auth.auth().currentUser? is an optional - meaning that it could have a value or could be nil. Doing this..



        let currentUserId = Auth.auth().currentUser?.uid


        is telling your code that no matter if its got a value or nil, attempt to assign the .uid to currentUserId.



        Protect your code by safely unwrapping options



        guard let currentUser = Auth.auth().currentUser else {return}
        //now you can access currentUser as it won't be nil


        or



        if let currentUser = Auth.auth().currentUser {
        print("user is authenticated and currentUser is not nil")
        } else {
        print("not authenticated")
        }


        Now the core of the issue is, as far as I can see, you're not authenticating the user at all and that needs to happen prior to accessing Auth.auth().currentUser



        Here's a complete authentication function with error handling



        func authUser(user: String, pw: String) {
        Auth.auth().signIn(withEmail: user, password: pw, completion: { (auth, error) in
        if let x = error {
        let err = x as NSError
        switch err.code {
        case AuthErrorCode.wrongPassword.rawValue:
        print("wrong password")
        case AuthErrorCode.invalidEmail.rawValue:
        print("invalued email")
        default:
        print("unknown error")
        }
        } else {
        if let user = auth?.user { //note; safely unwrap optional
        print("uid: (user.uid)")
        }
        }
        })
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 14:03









        JayJay

        19.5k43353




        19.5k43353
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440677%2fi-get-this-error-when-i-run-my-app-thread-1-fatal-error-unexpectedly-found-ni%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?