Reputation: 1
So every time I add a new entry to Firebase in the addItemViewController it segues back to ViewController but when doing this the app crashes and with this error: "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value". The error is on the line in ViewController.swift in ViewDidLoad with tableView.reloadData(). Any suggestions would be much appreciated! Thanks!
ViewController.swift
//
// ViewController.swift
// toDo
//
// Created by Jesse Brior on 10/5/18.
// Copyright © 2018 Jesse Brior. All rights reserved.
//
import UIKit
import Firebase
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var ref : DatabaseReference!
var items: [itemObjectModel] = []
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
tableView.reloadData()
self.navigationItem.setHidesBackButton(true, animated: true)
self.title = "To Do's"
storeKeys()
}
func storeKeys() {
ref.child("tasks").queryOrdered(byChild: "completed").observe(.value, with: { snapshot in
var newItems: [itemObjectModel] = []
for child in snapshot.children {
if let snapshot = child as? DataSnapshot,
let newTask = itemObjectModel(snapshot: snapshot) {
newItems.append(newTask)
self.items = newItems
self.tableView.reloadData()
}
}
self.items = newItems
self.tableView.reloadData()
})
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let item = items[indexPath.row]
cell.textLabel?.text = item.name.capitalized
if item.completed {
cell.detailTextLabel?.text = "Complete"
}else{
cell.detailTextLabel?.text = "Incomplete"
}
return cell
}
}
addItemViewController.swift
//
// addItemController.swift
// toDo-v2
//
// Created by Jesse Brior on 10/9/18.
// Copyright © 2018 Jesse Brior. All rights reserved.
//
import Foundation
import UIKit
import Firebase
class addItemController: UIViewController {
var ref : DatabaseReference!
@IBOutlet weak var addItemField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
self.title = "Add Item"
}
@IBAction func addItemPressed(_ sender: Any) {
if addItemField.text != "" {
let task = addItemField.text!
let completion = false
let newItem = itemObjectModel(name: task, completed: completion)
let newItemRef = self.ref.child("tasks").child(task.lowercased())
newItemRef.setValue(newItem.toAnyObject())
}
self.present(ViewController(), animated: true, completion: nil)
}
}
link to pic of crash log: https://i.sstatic.net/ZGC4k.png
Upvotes: 0
Views: 716
Reputation: 4711
You error is in this line:
self.present(ViewController(), animated: true, completion: nil)
That does not as you put it 'segue back' to you ViewController
class but it creates a new instance of it and presents that. Also as it is creating a new instance of just the basic class when it should probably be instantiating it from the storyboard/xib file and therefore it does not set up any of the IBOutlets just creates the plain empty class. So that is why you get the error that tableview
is nil, nothing has set it up.
You don't show how you present the addItemController
as that will dictate the correct method to use to return back to your original ViewController
.
On a slightly separate note you should really use the correct format for class and variable names in Swift. So addItemController
is a class and should therefore be called AddItemController
and in the ViewController
the tableview
should really be tableView
.
Upvotes: 0