私は iOS 開発を始めたばかりで、最初の良いアプリは単純な電卓だと思っていました。残念ながら、アプリを実行しようとすると、「このクラスはキー numberFour のキー値コーディングに準拠していません」というエラーでクラッシュします。
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
ここに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