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

2

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

My IBOutlet isn't showing in there at all. I don't understand why it's not appearing : >
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.