-1

I'm just starting out with iOS development and thought a good first app would be a simple calculator. Unfortunately, whenever I try to run the app it crashes with the error "this class is not key value coding-compliant for the key numberFour".

Here is ViewController.h:

#import <UIKit/UIKit.h>

 @interface ViewController : UIViewController
- (IBAction)numberZero:(id)sender;
- (IBAction)numberOne:(id)sender;
- (IBAction)numberTwo:(id)sender;
- (IBAction)numberThree:(id)sender;
- (IBAction)numberFour:(id)sender;
- (IBAction)numberFive:(id)sender;
- (IBAction)numberSix:(id)sender;
- (IBAction)numberSeven:(id)sender;
- (IBAction)numberNine:(id)sender;
- (IBAction)numberEight:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *expressionView;

-(void)updateExpressionView; 

@property NSNumber *operandOne;
@property NSNumber *operandTwo;
@property NSNumber *one;
@property NSNumber *two;
@property NSNumber *three;
@property NSNumber *four;
@property NSNumber *five;
@property NSNumber *six;
@property NSNumber *seven;
@property NSNumber *eight;
@property NSNumber *nine;
@property NSNumber *zero;

@property NSMutableArray *expression; 

@end

Here is ViewController.m:

#import "ViewController.h"



@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad
 {
    [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
     self.one = [NSNumber numberWithInt: 1];
     self.two = [NSNumber numberWithInt:2];
     self.three = [NSNumber numberWithInt:3];
     self.four = [NSNumber numberWithInt: 4];
     self.five = [NSNumber numberWithInt:5];
     self.six = [NSNumber numberWithInt: 6];
     self.seven = [NSNumber numberWithInt:7];
     self.eight = [NSNumber numberWithInt:8];
     self.nine = [NSNumber numberWithInt: 9];
     self.zero = [NSNumber numberWithInt:0];

 }

- (void)didReceiveMemoryWarning
{
     [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)numberOne:(id)sender {
    [self.expression addObject:self.one];
    [self updateExpressionView];

}

 - (IBAction)numberTwo:(id)sender {
     [self.expression addObject: self.two];
     [self updateExpressionView];


}

- (IBAction)numberThree:(id)sender {
    [self.expression addObject: self.three];
    [self updateExpressionView];

}

- (IBAction)numberFour:(id)sender {
     [self.expression addObject: self.four];
     [self updateExpressionView];

      }

  - (IBAction)numberFive:(id)sender {
     [self.expression addObject: self.five];
     [self updateExpressionView];


  }

  - (IBAction)numberSix:(id)sender {
      [self.expression addObject:self.six];
      [self updateExpressionView];

  } 

 - (IBAction)numberSeven:(id)sender {
     [self.expression addObject: self.seven];
     [self updateExpressionView];

 }

 - (IBAction)numberNine:(id)sender {
     [self.expression addObject: self.nine];
     [self updateExpressionView];

 }
 - (IBAction)numberZero:(id)sender {
     [self.expression addObject: self.zero];
     [self updateExpressionView];

 }

 - (IBAction)numberEight:(id)sender {
     [self.expression addObject: self.eight];
     [self updateExpressionView];

 }

 -(void)updateExpressionView {
      NSString *expressionNSString = (NSString *)self.expression;
      self.expressionView.text = expressionNSString;
  }
 @end
blockaj
  • 329
  • 4
  • 13
  • See http://stackoverflow.com/questions/13793162/setvalueforundefinedkey-this-class-is-not-key-value-coding-compliant-for-the-k/13793334#13793334 – AlexWien Oct 03 '13 at 19:39
  • Please look at all of the duplicate questions listed under the "Related" section of your question. – rmaddy Oct 03 '13 at 21:14

4 Answers4

1

In your xib or storyboard, the scene for this does not have the type for the View Controller specified correctly. You can find this in the interface builder, when you select the scene for your calculator, and inspect its properties (see attached image). You need to set that class to the name of your custom class that inherits from UIViewController (in this case, ViewController).

View controller attributes

RyanR
  • 7,728
  • 1
  • 25
  • 39
0

You don't have numberFour connected in your .xib or storyboard file or more likely the files owner isn't of type ViewController.

ahwulf
  • 2,584
  • 15
  • 29
0

Sorry for not adding this as a comment, I don't have enough reputation to do so (min. 50 required).

This question's been asked before, look on SO: What does this mean? "'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X" is one example.

  1. Have you checked your view (in your storyboard) uses the correct controller ViewController?
  2. Have you correctly connected the IBAction in your Storyboard with the actual view? (If nr.1 is correct this normally shouldn't really matter in my experience).
Community
  • 1
  • 1
testuser
  • 952
  • 4
  • 17
  • 34
0

Are you sure all your IBAction are links on your xib or view in storyboard ?

You need also to initialize your array before adding object inside :

self.expression = [[NSMutableArray alloc] init]; // add this line just after [super viewDidLoad];
Jordan Montel
  • 8,227
  • 2
  • 35
  • 40