Until recently, I have no had a problem to be able to pull my data from the database - and only when I decided to add another variable (totalDistance) did it start to break, however when attempting to remove this newly written code, the error stayed.
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
this is the error I am getting when I attempt to go onto the Profile page which is causing the error, here is the function I have wrote to be able to write the labels -
let ref = FIRDatabase.database().reference()
let UserID = FIRAuth.auth()?.currentUser?.uid
ref.child("user").child(UserID!).observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let displayname = value?["nickname"] as? String ?? "Nickname Missing"
let bronzeMedals = value?["bronzeMedals"] as? String ?? "no bronze in db"
let silverMedals = value?["silverMedals"] as? String ?? "no silver in db"
let goldMedals = value?["goldMedals"] as? String ?? "no gold in db"
let totalDistance = value?["totalDistance"] as? String ?? "no total in db"
print(bronzeMedals, silverMedals, goldMedals)
self.displaynameLabel.text = ("Username: \(displayname)")
self.goldmedalsLabel.text = ("Gold medals: \(goldMedals)")
self.silvermedalsLabel.text = ("Silver medals: \(silverMedals)")
self.bronzemedalsLabel.text = ("Bronze medals: \(bronzeMedals)")
self.totaldistanceLabel.text = ("Total distance: \(totalDistance)")
})
This method of calling the informaiton has worked flawlessy until I had attempted to add another variable, I am very confused by this error and cannot seem to find out how to fix it - no matter how many different errors I look up which have already been answered, so any information will be very essential and well appreciated.
Here is the database file of the user - which is being called,
age:
"18"
bronzeMedals:
"2"
goldMedals:
"3"
nickname:
"Test Account"
silverMedals:
"5"
totalDistance:
"2"