title: Terminating app due to uncaught exception 'RLMException', reason: 'Index 0 is out of bounds (must be less than 0).' Swift
body: <p>I am trying to make a productivity app. The code is as follows:</p>
  <pre><code>import Foundation
  import RealmSwift
  
  class TimeCardData: Object {
      @objc dynamic var clockIn3: String = &quot;&quot;
      @objc dynamic var clockOut3: String = &quot;&quot;
      @objc dynamic var minutes3: Int = 0
      @objc dynamic var productivity3: String = &quot;&quot;
      @objc dynamic var lunch3: String = &quot;&quot;
      @objc dynamic var groupNumber3: String = &quot;&quot;
      @objc dynamic var group3: String = &quot;&quot;
      @objc dynamic var timeWorked3: String = &quot;&quot;
      @objc dynamic var clockInDate3 = &quot;&quot;
      @objc dynamic var hoursWorked3: String = &quot;&quot;
  }
  
  class TimeCardViewController: UIViewController {
      
      let realm = try! Realm()
      
      var timeCardData : Results&lt;TimeCardData&gt;?
      
      @IBOutlet weak var searchBar: UISearchBar!
      @IBOutlet weak var tableView: UITableView!
      
      var clockInDate2 = &quot;&quot;
      var hoursWorked2 = &quot;&quot;
      var timeWorked2 = &quot;&quot;
      var productivity2 = &quot;&quot;
      var clockIn2 = &quot;&quot;
      var clockOut2 = &quot;&quot;
      var lunch2 = &quot;&quot;
      var minutes2 = 0
      var groupNumber2 = &quot;&quot;
      var group2 = &quot;&quot;
      
      override func viewDidLoad() {
          super.viewDidLoad()
          
          tableView.delegate = self
          tableView.dataSource = self
          tableView.rowHeight = 100.0
          loadTimeCardData()
  
      }
      
      func saveTimeCardData(timeCardData: TimeCardData) {
          do {
              try realm.write {
                  realm.add(timeCardData)
              }
          } catch {
              print(&quot;Error saving TimeCard Data \(error)&quot;)
          }
          
          tableView.reloadData()
      }
      
      func loadTimeCardData() {
          
          timeCardData = realm.objects(TimeCardData.self)
          tableView.reloadData()
      }
  }
  
  extension TimeCardViewController: UITableViewDataSource, UITableViewDelegate {
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          
          return timeCardData?.count ?? 1
              
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell =  tableView.dequeueReusableCell(withIdentifier: &quot;TimeCardCell&quot;, for: indexPath) as! TimeCardTableViewCell
          
          cell.productivity?.text = timeCardData?[indexPath.row].productivity3
          cell.hoursWorked?.text = timeCardData?[indexPath.row].hoursWorked3
          cell.clockInDate?.text = timeCardData?[indexPath.row].clockInDate3
          cell.timeWorked?.text = timeCardData?[indexPath.row].timeWorked3
          
          return cell
      }
      
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          
          let TCD = TimeCardData()
              TCD.clockIn3 = clockIn2
              TCD.clockOut3 = clockOut2
              TCD.group3 = group2
              TCD.lunch3 = lunch2
              TCD.minutes3 = minutes2
              TCD.groupNumber3 = groupNumber2
              TCD.productivity3 = productivity2
              TCD.timeWorked3 = timeWorked2
              TCD.clockInDate3 = clockInDate2
              TCD.hoursWorked3 = hoursWorked2
              
              saveTimeCardData(timeCardData: TCD)
          
      }
      
      func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
          if editingStyle == .delete {
              
              do {
                         try realm.write {
                          let cellToBeDeleted = timeCardData![indexPath.row]
                          realm.delete(cellToBeDeleted)
                         }
                     } catch {
                         print(&quot;Error deleting TimeCard Data \(error)&quot;)
                     }
          }
      }
  }
  
  </code></pre>
  <p>The problem is on <code>numberOfRowsInSection</code>, when I <code>return timeCardData?.count ?? 1</code>, it is not bringing any cells on my table. I presume it returns 0 and therefore it does not bring a cell. If I just return 1 or any other number or anything other than the above, the app crashes and gives me the 'Index 0 is out of bounds (must be less than 0).' reason for crashing. This is one of my first apps and I am stuck and do not know how to get around this. What am I doing wrong and how can I proceed from this point onwards?</p>