I keep getting this error: 

> 
> 2017-10-18 22:57:52.401421+0300 Expense Manager[4213:133067] **\* Terminating app due to uncaught exception 'NSRangeException', reason: '\*** -[\_\_NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'
> 
> 
> 

But I have no idea what did I do wrong. The **tableView** works just fine when **numberOfSections** is **1** and they are not sorted by date, but when I try and do sections for each day, it crashes on app launch.

```
  func userBudgetCount(_ section: Int) -> Int {
            return fetchedResultsController.sections![section].numberOfObjects
        }

        func getUserBudgetAtIndexPath(indexPath : IndexPath) -> Budget {
            return fetchedResultsController.object(at: indexPath) as Budget
        }

        override func viewDidLoad() {
            super.viewDidLoad()
            self.hideKeyboard()

            tableView.delegate = self
            tableView.dataSource = self

            self.tableView.tableFooterView = UIView()

        }

        override func viewDidAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            fetchCoreDataObject()
            tableView.reloadData()

        }

        func fetchCoreDataObject() {
            self.fetch { (complete) in
                if complete {
                    if userBudgetCount(0) >= 1 {
                        userBudgetLabel.text = replaceLabel(number: userMoney[userMoney.count - 1].userMoney)
                        tableView.isHidden = false
                        plusButton.isHidden = false
                        moreBtn.isHidden = false
                    } else {
                        tableView.isHidden = true
                        userBudgetLabel.text = "Bugetul tau"
                        plusButton.isHidden = true
                        moreBtn.isHidden = true
                    }
                }
            }
        }

        var fetchedResultsController: NSFetchedResultsController<Budget> {
            if _fetchedResultsController != nil {
                return _fetchedResultsController!
            }

            let fetchRequest = NSFetchRequest<Budget>(entityName: "Budget")

            // Set the batch size to a suitable number.
            fetchRequest.fetchBatchSize = 20

            // Edit the sort key as appropriate.
            let sortDescriptor = NSSortDescriptor(key: "dateSubmitted" , ascending: false)

            fetchRequest.sortDescriptors = [sortDescriptor]

            // Edit the section name key path and cache name if appropriate.
            // nil for section name key path means "no sections".
            let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext!, sectionNameKeyPath: "dateSection", cacheName: nil)
            aFetchedResultsController.delegate = self
            _fetchedResultsController = aFetchedResultsController

            do {
                try _fetchedResultsController!.performFetch()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
            }

            return _fetchedResultsController!
        }
        var _fetchedResultsController: NSFetchedResultsController<Budget>? = nil

        func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
            tableView.reloadData()
        }
     func numberOfSections(in tableView: UITableView) -> Int {
            return fetchedResultsController.sections!.count
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "expenseCell") as? ExpenseCell else { return UITableViewCell() }
            let budget = getUserBudgetAtIndexPath(indexPath: indexPath)
            cell.delegate = self
            cell.configureCell(budget: budget)
            return cell
        }

        func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
            return true
        }

        func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
            return UITableViewCellEditingStyle.none
        }

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return userBudgetCount(section)
        }

        func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            return "Section: \(section)"
        }

```