0

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)
}
Radu Diță
  • 13,476
  • 2
  • 30
  • 34
Jack W
  • 1
  • Have you tried setting an exception breakpoint to see where the error is triggered? That will help with debugging it. – Dru Freeman Apr 06 '17 at 15:50
  • This error often happens when using storyboards. Do you have any `@IBOutlet` or `@IBAction` in the view controller? Or this a full code? – inokey Apr 06 '17 at 16:02
  • Possible duplicate of [What does this mean? "'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X"](http://stackoverflow.com/questions/3088059/what-does-this-mean-nsunknownkeyexception-reason-this-class-is-not-key-v) – rmaddy Apr 06 '17 at 16:07
  • Another suggestion: print the dictionary. Are you really getting the data? – inokey Apr 06 '17 at 16:08
  • @LordAndrei - I tried. Can't seem to find anything. Only way I stopped the crash is when I removed the function 'observeMessages()'. – Jack W Apr 07 '17 at 12:33
  • @inokey - I didn't use a storyboard. I've coded it from scratch! Not sure what you mean otherwise. – Jack W Apr 07 '17 at 12:33
  • @JackW did you use xib for your custom cell? – inokey Apr 07 '17 at 12:37
  • @inokey I didn't use xib. – Jack W Apr 07 '17 at 15:13
  • @JackW I think your problem might be hidden here `message.setValuesForKeysWithDictionary(dictionary)` do you really get the data there? And what does this method do actually. Edit with code please. – inokey Apr 07 '17 at 15:21

1 Answers1

1

cell.detailTextLabel?.text = message.text

Error is in this line of code make sure message contains Subclass text Reason behind: Compiler is not able to find such a key in mesage

Saranjith
  • 11,242
  • 5
  • 69
  • 122
  • Could you explain a little more with what you mean, please? – Jack W Apr 07 '17 at 12:30
  • anyway this message is having some properties right? you're trying to access the property text.. which is not there,, so it is crashing,, i hop you get what i mean – Saranjith Apr 07 '17 at 12:33