3

New to Objective-C. I can build my app successfully but then I get the following error:

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

I checked each button on the page with the connection inspector and I didn't see any warnings. I may have added a connection and then deleted it incorrectly so it's still somewhere in the background, is there a way to see all connections?. I'm not really sure.

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;


- (void)updateUI
{
    for (UIButton *cardButton in self.cardButtons) {
        int cardIndex = [self.cardButtons indexOfObject:cardButton];
        Card *card = [self.game cardAtIndex:cardIndex];
        [cardButton setTitle:[self titleForCard:card]
                    forState:UIControlStateNormal];
        [cardButton setBackgroundImage:[self backgroundImageForCard:card]
                              forState:UIControlStateNormal];
        cardButton.enabled = !card.isMatched;
    }
    self.scoreLabel.text = [NSString stringWithFormat:@"Score: %d", self.game.score];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2954587
  • 4,661
  • 6
  • 43
  • 101

2 Answers2

4

this class is not key value coding-compliant for the key cardButton

So the key that the error mentions is cardButton (no 's'), but your code uses the property cardButtons. It sounds like you simply misspelled the property name somewhere, leaving off the 's'.

I may have added a connection and then deleted it

It's easy to get this error if you previously used the name cardButton for your property, perhaps before you switched to a collection. If you left an object connected to that property in your .xib or storyboard, iOS will try to connect that object to the property cardButton when it's loaded, resulting in exactly the run-time error that you're getting. You don't see any warnings in Xcode because the problem isn't in your code. So, check your .xib or storyboard files for an object connected to a cardButton property.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • cardButtons and cardButton are spelled correctly. cardButtons is the array, and cardButton is a representative button from that array. – Kaveh Vejdani Apr 23 '14 at 22:34
  • @KavehVejdani I see that, but given the error it's clear that the OP is somewhere trying to use the key `cardButton`, and the class in question doesn't support that key. The posted code doesn't seem to be the issue, but the mistake may be elsewhere in the code or, as I suggested, it may be a connection in a .xib or storyboard file. – Caleb Apr 24 '14 at 00:45
  • I agree. His question is: "is there a way to see all connections?" – Kaveh Vejdani Apr 24 '14 at 01:01
2
  1. In your storyboard view, click the file owner button at the bottom of your viewcontroller's view window, inside the black control bar.

  2. In the object inspector's window on the right side of the screen, click the connections inspector, i.e the little right-arrow on the top right corner. There you see all the connections you are looking for.

enter image description here

Kaveh Vejdani
  • 224
  • 1
  • 6