This class is not key value coding-compliant for the key...why?

12,930

Solution 1

I linked to the WRONG Source!

enter image description here

Here's the result:

enter image description here

Solution 2

Show the references of your ViewController rigth-clicking in it on the Document Outline. Probably you will see a warning in one of the references. Delete it and link it again if still need it.

enter image description here

Solution 3

You should not be calling instantiateWithOwner yourself inside tableView:cellForRowAtIndexPath.

Register the nib in viewDidLoad and then dequeueReusableCellWithIdentifier will do all the work for you.

The reason for your particular error is that you are calling instantiateWithOwner passing self as the owner and so the nib is trying to wire the outlets up to your UITableViewDataSource implementation class rather than a DiaryTableViewCell.

Share:
12,930
Frederick C. Lee
Author by

Frederick C. Lee

My raison d'être: To exploit the power of Apple (iOS/OSX) and open-source technologies. To promote intuitive client/server paradigms using the distributive ‘cloud computing’ paradigm whilst leveraging the power of Apple’s iOS. I enjoy cool clean air and water in tranquility. I enjoy aquaria. I admire well-written works of literature & logic; and the arts...as I do cuisine.

Updated on June 05, 2022

Comments

  • Frederick C. Lee
    Frederick C. Lee almost 2 years

    I've linked output from the IB to the code, as shown below.

    class DiaryTableViewCell: UITableViewCell {
        @IBOutlet weak var TitleLabel: UILabel!
        @IBOutlet weak var SubTitleLabel: UILabel!
        @IBOutlet weak var leftImageView: UIImageView!
        @IBOutlet weak var rightImageView: UIImageView!
    }
    

    Here, I'm registering the class:

    override func viewDidLoad() {
       self.title = "My Diary"
       cellNib = UINib(nibName: "TableViewCells", bundle: nil)
       tableView.registerClass(DiaryTableViewCell.classForCoder(), forCellReuseIdentifier: kCellIdentifier)
    }
    

    enter image description here

    enter image description here

    But I keep getting the following runtime error:

    *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '...setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key SubTitleLabel.'

    From within the following code:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
    var cell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as DiaryTableViewCell?
    
    if (cell == nil) {
        tableView.registerClass(DiaryTableViewCell.classForCoder(), forCellReuseIdentifier: kCellIdentifier)
        cell = cellNib?.instantiateWithOwner(self, options: nil)[0] as? DiaryTableViewCell
                    cell?.selectionStyle = .None
    }
    
    if (cell != nil) {
        println("\(x++)) Inside cell")
        cell!.TitleLabel.text = "Hello"
        cell!.SubTitleLabel.text = "World"
    }
    
    return cell!
    }
    

    Specifically, it's happening here:

    cell = cellNib?.instantiateWithOwner(self, options: nil)[0] as? DiaryTableViewCell
    

    Question: How am I violating the key value coding-compliant for a UILabel?

    This hasn't happened before... UILabel is KVO compliant.