title: Retrieving data from Firebase equals unexpected nil - swift
body: <p>I get the weirdest error and I do not know how to fix it..
  I have a Firebase database with all posts made from users. They are stored under <code>feed-items</code>. Whenever a user makes a new post, the post is added to <code>feed-items</code> and the key of the post in feed-items (an autoId) is added to every follower that the user has.</p>
  
  <p>If I show all posts from <code>feed-items</code> in my UITableView then there is no problem at all. If I try to show posts that are equal to the keys stored in the users personal wall (the key that was added when users that I am following makes a post), then I get a weird error saying <code>unexpectedly found nil while unwrapping an optional</code> in my <code>Sweet</code> struct?</p>
  
  <p>Let me first show you the two codes, one working the other is not:</p>
  
  <p><strong>WORKING CODE:</strong></p>
  
  <pre><code>func observePosts(userID: String) {
  //        let ref = FIRDatabase.database().reference().child("Users").child(userID).child("Wall")
  //        ref.observeEventType(.ChildAdded, withBlock: { (snapshot) in
  //            
  //            let postId = snapshot.key
              let postReference = FIRDatabase.database().reference().child("feed-items")//.child(postId)
  
              postReference.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
                  print(snapshot)
                  var newUpdates = [Sweet]()
                      for update in snapshot.children {
                          let postUpdates = Sweet(snapshot: update as! FIRDataSnapshot)
                          newUpdates.append(postUpdates)
                      }
                      self.updates = newUpdates.reverse()
  
                      dispatch_async(dispatch_get_main_queue(), {
                         self.tableView.reloadData()
                      })
                  }, withCancelBlock: nil)
              //}, withCancelBlock: nil)
      }
  </code></pre>
  
  <p><strong>NOT WORKING CODE:</strong></p>
  
  <pre><code>func observePosts(userID: String) {
          let ref = FIRDatabase.database().reference().child("Users").child(userID).child("Wall")
          ref.observeEventType(.ChildAdded, withBlock: { (snapshot) in
  
              let postId = snapshot.key
              let postReference = FIRDatabase.database().reference().child("feed-items").child(postId)
  
              postReference.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
                  print(snapshot)
                  var newUpdates = [Sweet]()
                      for update in snapshot.children {
                          let postUpdates = Sweet(snapshot: update as! FIRDataSnapshot)
                          newUpdates.append(postUpdates)
                      }
  
                      self.updates = newUpdates.reverse()
  
                      dispatch_async(dispatch_get_main_queue(), {
                         self.tableView.reloadData()
                      })
                  }, withCancelBlock: nil)
              }, withCancelBlock: nil)
      }
  </code></pre>
  
  <p>Where the app crashes and the line I get the error is in my struct, under likesForPost</p>
  
  <p><strong>MY STRUCT</strong></p>
  
  <pre><code>import Foundation
  import FirebaseDatabase
  import FirebaseAuth
  import UIKit
  
  struct Sweet {
      let key: String!
      let content: String!
      let addedByUser: String!
      let profilePhoto: String!
      var likesForPost : [String:AnyObject]
      let itemRef: FIRDatabaseReference?
      let path : String!
      let date: String!
      let category: Int!
      let workoutComment: String!
      let workoutTime: String!
  
  
      init (content: String, addedByUser: String, profilePhoto: String!, likesForPost : [String:AnyObject]!, date: String, category: Int, workoutComment: String, workoutTime: String, key: String = "") {
          self.key = key
          self.content = content
          self.addedByUser = addedByUser
          self.profilePhoto = profilePhoto
          self.likesForPost = likesForPost
          self.itemRef = nil
          // self.path = dataPath
          self.path = ""
          self.date = date
          self.category = category
          self.Comment = Comment
          self.Time = Time
      }
  
      init (snapshot: FIRDataSnapshot) {
          key = snapshot.key
          itemRef = snapshot.ref
          path  = key
          if let theFeedContent = snapshot.value!["content"] as? String {
              content = theFeedContent
          } else {
              content = ""
          }
  
          if let feedUser = snapshot.value!["addedByUser"] as? String {
              addedByUser = feedUser
          } else {
              addedByUser = ""
          }
  
          if let feedPhoto = snapshot.value!["profilePhoto"] as? String! {
              profilePhoto = feedPhoto
          } else {
              profilePhoto = ""
          }
  
          if let feedLikes = snapshot.value!["likesForPost"] as? [String:AnyObject]! {
              likesForPost = feedLikes
          } else {
              likesForPost = ["":""]
          }
  
          if let feedDate = snapshot.value!["date"] as? String! {
              date = feedDate
          } else {
              date = ""
          }
  
          if let feedCategory = snapshot.value!["category"] as? Int! {
              category = feedCategory
          } else {
              category = 0
          }
  
          if let feedComment = snapshot.value!["Comment"] as? String! {
              Comment = feedComment
          } else {
              Comment = ""
          }
  
          if let feedTime = snapshot.value!["Time"] as? String! {
              Time = feedTime
          } else {
              Time = ""
          }
  
      }
  
      func toAnyObject() -&gt; AnyObject {
          return ["content":content, "addedByUser":addedByUser, "profilePhoto":profilePhoto!, "likesForPost":likesForPost, "date":date, "category":category, "Comment":Comment, "Time":Time]
      }
  }
  </code></pre>
  
  <p>I know it is a long post but I would be so happy if you took the time to help me - I am completely lost! :(</p>
  
  <p><strong>EDIT:</strong>
  This is what the log is giving me:</p>
  
  <pre><code>Snap (-KXLGyWMljc2UNeRL1a3) {
      addedByUser = "Maja P";
      category = 1;
      content = Hejehjehj;
      date = "Nov,24,,2016,,1:20";
      likesForPost =     {
          "user id" = 0;
      };
      profilePhoto = VGpbOweP4jO2seeLNCGiv1p08jq1;
      Comment = "";
      Time = "";
  }
  fatal error: unexpectedly found nil while unwrapping an Optional value
  </code></pre>
  
  <p>Which is completely correct, but there are two posts where it is only printing the one above.</p>