-2

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"
Gary Tate
  • 11
  • 1
  • 5
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – xoudini Apr 10 '17 at 12:20
  • Likely `ref.child("user").child(UserID!)` causing the crash due to `UserID` being `nil`. Don't force unwrap. – xoudini Apr 10 '17 at 12:21
  • Knowing what line is crashing would be helpful. – Jay Apr 10 '17 at 18:25

1 Answers1

0

You really need to change you code so that you are not working with optionals.

let ref = FIRDatabase.database().reference()

guard let user = FIRAuth.auth()?.currentUser else { return }

ref.child("user").child(user.uid).observeSingleEvent(of: .value, with: { (snapshot) in

    guard let value = snapshot.value as? Dictionary<String,String> else { return }

    let displayname = value["nickname"] ?? "Nickname Missing"

    let bronzeMedals = value["bronzeMedals"] ?? "no bronze in db"

    let silverMedals = value["silverMedals"] ?? "no silver in db"

    let goldMedals = value["goldMedals"] ?? "no gold in db"

    let totalDistance = value["totalDistance"] ?? "no total in db"


    // Update Labels

    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)")
})
Tristan Beaton
  • 1,742
  • 2
  • 14
  • 25