0

错误

实例变量已声明

类型名称需要说明符或限定符

预期的 ';' 在声明列表的末尾

我无法弄清楚为什么会这样。请帮忙!

这是我的代码

视图控制器.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
4

2 回答 2

2

您错误地声明了数组。将其从头部完全删除,digits仅在实现文件(.m 文件)中声明 char 数组。然后错误应该消失。此外,您分配的值不正确。您正在创建一个包含 10 个元素的 char 数组,然后*将另一个 char 数组分配给第 10 个索引处的元素,无论如何它都不存在. There are multiple errors there. It should bedigits = {...} notdigits[10] = {...}` .

如果我是你,我会先开始学习纯 C 并获得基础知识,然后再进入 Objective-C,因为它只会更复杂。我看到您想创建 iOS 应用程序,但要使该过程更容易,您首先需要了解 C 的基础知识。

于 2013-08-15T16:37:56.000 回答
0

我的措辞在这里可能是错误的,但您正在界面中初始化数字。做这个。

您可以在 .m 文件中执行此操作(并且不需要 @interface evalViewController() @end 只是向您显示放置它的位置)

char digits[10] = { ... };

@interface evalViewController ()

@end
于 2013-08-15T17:03:11.123 回答