1

I have a function to get certain posts from Firebase

    var posts = NSMutableArray()

    override func viewDidLoad() {
        super.viewDidLoad()
        FIRDatabase.database().reference().child("Likes").child(self.loggedInUser!.uid).observe(.childAdded, with: { (snapshot) in


            self.ID = snapshot.key
            print(self.ID!)
        })


        loadData()

}
   func loadData(){

    FIRDatabase.database().reference().child("books").child(self.ID!).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in

            if let postsDictionary = snapshot .value as? [String: AnyObject] {
                for post in postsDictionary {
                    self.posts.add(post.value)
                }
                self.SoldTableView.reloadData()

            }})


}

Now I get an error on

FIRDatabase.database().reference().child("books").child(self.ID!).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in

saying fatal error: unexpectedly found nil while unwrapping an Optional value

KENdi
  • 7,576
  • 2
  • 16
  • 31
juelizabeth
  • 485
  • 1
  • 8
  • 31
  • Possible duplicate of https://stackoverflow.com/questions/40459460/can-not-cast-value-of-type-nstaggedpointerstring-to-nsdictionary – Mike Taverne Oct 25 '17 at 01:21
  • Some value its a non-string attribute and you are casting only to strings, I would bet that Price is a float or int value in your Firebase db, `cell.Price.text = (post["Price"] as! NSString).doubleValue` might fix the problem – Karlo A. López Oct 25 '17 at 01:34
  • @KarloA.López no that didn;t work – juelizabeth Oct 25 '17 at 01:37
  • All your values are actually Strings? `cell.Price.text = "\(post["Price"])"` try displaying data as an appended text – Karlo A. López Oct 25 '17 at 01:43
  • @KarloA.López I changed my question but I am still having trouble – juelizabeth Oct 25 '17 at 02:05
  • I think you should use callback/completion handler for this problem or you can put your `loadData()` inside closure after `self.ID = snapshot.key` like this `self.loadData()` – 3stud1ant3 Oct 25 '17 at 02:31

1 Answers1

0

It seems you are force unwrapping an optional (self.ID), and it is nil which is why it is throwing an error.

You should unwrap the optional safely before using the value:

if let ID = self.ID {
        FIRDatabase.database().reference().child("books").child(ID).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in

        if let postsDictionary = snapshot .value as? [String: AnyObject] {
            for post in postsDictionary {
                self.posts.add(post.value)
            }
            self.SoldTableView.reloadData()

        }})
}

Refer to this for more information on optionals: What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?

  • now I get no error and nothing is displayed in my table view so I'm not sure how to go from there. This is what I am trying to accomplish; I have a node called like and in that node, there are ID's of posts the loggedinuser likes. I want to get those IDs and go into the books node, look for them and display them in my table view. – juelizabeth Oct 25 '17 at 14:31