title: fatal error: Index out of range, when passing FirebaseData to tablviewCell
body: <p>so I am working a food list project that I want to pass mealname/mealpic/mealDescription to my tableViewCell from FirebaseDatabase.</p>
  
  <p>it will be something looks like this <a href="https://i.stack.imgur.com/mo8J1.png" rel="nofollow">enter image description here</a></p>
  
  <p>here's my FirebaesDatabase Structure </p>
  
  <p><a href="https://i.stack.imgur.com/vkJbM.png" rel="nofollow">enter image description here</a></p>
  
  <p>and here's my code in the view controller</p>
  
  <pre><code>    override func viewDidLoad() {
      super.viewDidLoad()
  
      WeekMenu.delegate = self
      WeekMenu.dataSource = self
  
      let url = GIDSignIn.sharedInstance().currentUser.profile.imageURL(withDimension: 100)
      let data = try? Data(contentsOf: url!)
      userpic.image = UIImage(data:data!)
  
  
  
      DataService.ds.REF_MENUDATA.observe(.value, with: { (snapshot) in
          if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot]
  
          {
              for snap in snapshot {
                  print("SNAP: \(snap)")
                  if let menuDict = snap.value as? Dictionary&lt;String, AnyObject&gt; {
                      let date = snap.key
                      let menu = menuContnet(weekmenudate: date, menuData: menuDict as! Dictionary&lt;String, String&gt;)
                      self.menus.append(menu)
                  }
              }
          }
      })
      self.WeekMenu.reloadData()
  }
  func numberOfSections(in tableView: UITableView) -&gt; Int {
      return 1
  }
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return 5
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let menu = menus [indexPath.row]
      if let cell: WeekMenu = tableView.dequeueReusableCell(withIdentifier: "WeekMenu") as! WeekMenu {
    cell.configureCell(menu: menu)
      return cell
      }  
  }
  
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      performSegue(withIdentifier: "ToVari", sender: nil)  
      }
  </code></pre>
  
  <p>It crashed at runtime and throws an "Index out of range" error at line <code>let menu = menus [indexPath.row]</code></p>
  
  <p>I know this Index out of range error question is kind of FAQ
  but I did some research myself and they don't really help </p>
  
  <p>could anyone possibly point out what might went wrong ?
  thanks  </p>
  
  <p><strong>UPDATE</strong> </p>
  
  <p>now I successfully pass what I have in FirebaseDatabase into my tableview.</p>
  
  <p>but I have 5 nodes from Day1 to Day5 but my tableview shows only one (Day3)</p>
  
  <p>in my
   <code>func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return menus.count
      }</code></p>
  
  <p>why is that happening ?</p>
  
  <p>my class of content is like the following </p>
  
  <pre><code>class menuContnet {
  
  
  private var _menuKey: String!
  private var _mealDes : String!
  private var _mealPic : String!
  private var _mealname : String!
  private var _menuRef: FIRDatabaseReference!
  
  
  var menuKey: String{
      return _menuKey
  }
  
  var mealDes : String {
      return _mealDes
  }
  
  var mealname : String {
      return _mealname
  }
  
  var mealPic : String {
      return _mealPic
  }
  
  init (mealDes: String, mealname: String, mealPic: String) {
      self._mealDes = mealDes
      self._mealname = mealname
      self._mealPic = mealPic
  }
  
  init (menuKey: String, menuData : Dictionary &lt;String, String&gt;) {
      self._menuKey = menuKey
  
      if let mealname = menuData["mealname"]  {
          self._mealname = mealname
      }
  
      if let mealPic = menuData["mealPic"] {
          self._mealPic = mealPic
      }
  
      if let mealDes = menuData["mealDes"] {
          self._mealDes = mealDes
      }
  
      _menuRef = DataService.ds.REF_MENUS.child(_menuKey)
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot]            
          {
              for snap in snapshot {
  
                  print ("SNAP: \(snap.key)")
  
                  if  let menuDict = snap.value as? Dictionary&lt;String, String&gt; {
                      let key = snap.key
                      let menu = menuContnet(menuKey: key, menuData: menuDict)
                      print (key)
                      self.menus.append(menu)
  
                      self.WeekMenu.reloadData()
  </code></pre>
  
  <p>so at  print ("SNAP: (snap.key)") i get all nodes from Day1 to Day 5 </p>
  
  <p>but at print (key) i get only Day3 </p>
  
  <p>i think  the "if let menuDict" statement is causing the problem </p>
  
  <p>and if i get rid of the "if" and make my code looks like this </p>
  
  <pre><code>                    let menuDict = snap.value as? Dictionary&lt;String, String&gt; 
                      let key = snap.key
                      let menu = menuContnet(menuKey: key, menuData: menuDict)
                      print (key)
                      self.menus.append(menu)
  
                      self.WeekMenu.reloadData()
  </code></pre>
  
  <p>it crashed at runtime throwing "unexpectedly found nil while unwrapping an Optional value" error </p>
  
  <p>I think I'm getting there, I will keep doing some more research </p>
  
  <p>any tips is appreciated</p>