0

I'm trying to retrieve values from an array and use them to populate a table view.

The issue is I'm getting this error:

NSUnknownKeyException', reason: '[<NSObject 0x7fb54b78e610> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key bookTitle.

My code:

Book Class

class Book: NSObject {

    var allBooks: [Book] = []

    var bookAuthor: String
    var bookTitle: String

    init (bookAuthor: String, bookTitle: String) {
        self.bookAuthor = bookAuthor
        self.bookTitle = bookTitle

        super.init()
    }

}

BookStore Class

    class BookStore: NSObject {

    var allBooks: [Book] = []

    func addBook(bookAuthor: String, bookTitle: String) {
        let newBook = Book(bookAuthor: bookAuthor, bookTitle: bookTitle)
        allBooks.append(newBook)
    }

}

AddBookViewController

class AddBookViewController: UIViewController {

    @IBOutlet weak var bookTitle: UITextField!
    @IBOutlet weak var bookAuthor: UITextField!

    let bookStore: BookStore

    init (bookStore: BookStore) {
        self.bookStore = bookStore

        super.init(nibName: "AddBookViewController", bundle: nil)

        navigationItem.title = "Add New Book"

    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @IBAction func importISBN(sender: AnyObject) {
    }

    @IBAction func saveNewBook(sender: AnyObject) {

        let author = self.bookAuthor.text!
        let title = self.bookTitle.text!
        println("Author: \(author) Title: \(title)")

        bookStore.addBook(author, bookTitle: title)
        println("\(self.bookStore.allBooks.count)")
      }

}

TableViewController

    class BookListViewController: UITableViewController {


    @IBAction func addNewBook (sender: UIButton) {

        let addBookVC = AddBookViewController(bookStore: bookStore)
        let navController = UINavigationController(rootViewController: addBookVC)
        navigationController!.pushViewController(addBookVC, animated: true)

    }

    let bookStore: BookStore

    init(bookStore: BookStore) {
        self.bookStore = bookStore
        super.init(nibName: nil, bundle: nil)

        navigationItem.title = "Books"

        let addBook = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "addNewBook:")

        navigationItem.rightBarButtonItem = addBook

    }


    required init!(coder aDecoder: NSCoder!) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.rowHeight = 44

        let nib = UINib(nibName: "ItemCell", bundle: nil)
        tableView.registerNib(nib, forCellReuseIdentifier: "ItemCell")

    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        dispatch_async(dispatch_get_main_queue()) {
            self.tableView.reloadData()
        }
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return bookStore.allBooks.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: indexPath) as! ItemCell

        let books = bookStore.allBooks[indexPath.row]
        cell.bookTitle.text = books.bookTitle

        return cell
    }
}

Edits:

enter image description here enter image description here enter image description here

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
aamck
  • 100
  • 1
  • 11
  • does your code crash on cellForRowAtIndexPath ? – Doro Jun 23 '15 at 14:43
  • 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) – jtbandes Aug 03 '15 at 06:52

1 Answers1

1

IT happens when you have wrong outlet connection from your interface builder. Try recreate outlets from you UITableViewCell and remove all wrong outlets ( they marked as '!' at the right panel)

enter image description here

Doro
  • 2,413
  • 2
  • 14
  • 26
  • My IBOutlet isn't showing in there at all. I don't understand why it's not appearing : > – aamck Jun 23 '15 at 15:18
  • OK I fixed it. When I created the ItemCell class, I had it create a Xib file along with it. Seems I should have not done that then created the Xib file separately then linked them all together. Thank you for your help, it got me thinking in the right direction! – aamck Jun 23 '15 at 15:42