错误
实例变量已声明
类型名称需要说明符或限定符
预期的 ';' 在声明列表的末尾
我无法弄清楚为什么会这样。请帮忙!
这是我的代码:
视图控制器.m
#import "evalViewController.h"
@interface evalViewController () {
digits[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; //Errors: Instance variable is already declared, Type name requires a specifier or a qualifier, Expected ';' at end of declaration list
}
@end
@implementation evalViewController
@synthesize terminal;
@synthesize expression;
//Add iEval prefix to output
-(void)writeToTerminal:(NSString *)string {
self.terminal.text = [NSString stringWithFormat:@"ConsoleCalc> %@", string];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Define operation characters
self.add = '+';
self.sub = '-';
self.mult = '*';
self.div = '/';
//Define digits to compare to digits in editableExpression
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)evaluate:(UITextField *)sender {
//Set user input to NSString editableExpression
NSString *editableExpression = self.expression.text;
//Loop through new NSString
for (int i=0; i < editableExpression.length; i++ ){
char charAtPosition = [editableExpression characterAtIndex:i];
//Check if there is a + sign anywhere in the expression and write "Addition!" if there is
if (charAtPosition == self.add){
if ([self checkForDigits]){
[self writeToTerminal:@"There are digits in this string!"];
}
else {
[self writeToTerminal:@"There are no digits in this string!"];
}
}
//Check if there is a - sign anywhere in the expression and write "Subtraction!" if there is
else if (charAtPosition == self.sub) {
[self writeToTerminal:@"Subtraction!"];
}
//Check if there is a * sign anywhere in the expression and write "Multiplication!" if there is
else if (charAtPosition == self.mult) {
[self writeToTerminal:@"Multiplication!"];
}
//Check if there is a / sign anywhere in the expression and write "Division!" if there is
else if (charAtPosition == self.div) {
[self writeToTerminal: @"Division!"];
}
}
}
//clear the terminal
- (IBAction)clearTerminal:(id)sender {
[self writeToTerminal:@""];
}
- (BOOL)checkForDigits {
NSString *editableExpression = self.expression.text;
for (int i = 0; i < editableExpression.length; i++){
char charAtPosition = [editableExpression characterAtIndex:i];
for (int c = 0; c < 10; c++ ){
if (charAtPosition == digits[c]){
return true;
}
else {
return false;
}
}
}
}
@end
视图控制器.h
#import <UIKit/UIKit.h>
@interface evalViewController : UIViewController {
char digits[10];
}
//Large text view
@property (strong, nonatomic) IBOutlet UITextView *terminal;
//User input
@property (strong, nonatomic) IBOutlet UITextField *expression;
//Evaluate expression
- (IBAction)evaluate:(UITextField *)sender;
- (IBAction)clearTerminal:(id)sender;
//Operation characters
@property char add;
@property char sub;
@property char mult;
@property char div;
//Change to information view
- (void)writeToTerminal: (NSString *) string;
- (BOOL)checkForDigits;
@end