I'm working on an iOS app, and I'm fairly new. I've been following 'Let's Build That App' to get a general understanding of Swift. Currently ran into an issue and can't get my head around it like the rest.
2017-04-06 16:32:17.968 GameOfChats[7679:602767]
*** Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<GameOfChats.Message 0x6080000f4580> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key text:.
I get that it's to do with something not being assigned. But I can't figure out how it relates. I'm not using a storyboard drag and drop method. I've hard coded it as apparently that makes organisation easier. Any help would be greatly appreciated!
class MessagesController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .Plain, target: self, action: #selector(handleLogout))
let image = UIImage(named: "Message-50")
navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: .Plain, target: self, action: #selector(handleNewMessage))
checkIfUserIsLoggedIn()
observeMessages()
}
var messages = [Message]()
func observeMessages() {
let ref = FIRDatabase.database().reference().child("messages")
ref.observeEventType(.ChildAdded, withBlock: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let message = Message()
message.setValuesForKeysWithDictionary(dictionary)
self.messages.append(message)
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
}, withCancelBlock: nil)
}
// puts text in front page, would show people u have messaged
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style:.Subtitle, reuseIdentifier: "cellId")
let message = messages[indexPath.row]
cell.textLabel?.text = message.toId
cell.detailTextLabel?.text = message.text
return cell
}
//handles new message action request
func handleNewMessage() {
let newMessageController = NewMessageController()
newMessageController.messagesController = self
let navController = UINavigationController(rootViewController: newMessageController)
presentViewController(navController, animated: true, completion: nil)
}