因未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[<gameofchats.message 0x6080000f4580 =“”> setValue:forUndefinedKey:

时间:2017-04-06 15:42:22

标签: swift

我正在开发iOS应用程序,而且我很新。我一直在关注'让我们构建那个应用程序'来全面了解Swift。目前遇到了一个问题,无法像其他人一样了解它。

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:.

我知道这与未被分配的事情有关。但我无法弄清楚它是如何相关的。我没有使用故事板拖放方法。我已经将其硬编码为显然使组织更容易。任何帮助将不胜感激!

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)
}

1 个答案:

答案 0 :(得分: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

相关问题