错误
已声明实例变量
类型名称需要说明符或限定符
预期';'在声明清单的末尾
我无法弄清楚为什么会这样。请帮忙!
这是我的代码:
ViewController.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
ViewController.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
答案 0 :(得分:2)
您正在声明数组错误。完全从标头中删除它,并仅在实现文件(.m文件)中声明digits
char数组。然后错误就会消失。此外,您正在错误地分配值。您正在创建一个包含10个元素的char数组,然后您将*另一个char数组分配给第10个索引处的元素,该数组始终不存在. There are multiple errors there. It should be
digits = {...} not
个数字[10] = {...}`。
如果我是你,我将首先开始学习普通C并在进入Objective-C之前获得基础知识,因为它只会使复杂化更复杂。我看到你想创建iOS应用程序,但为了简化这个过程,你首先需要了解C井的基础知识。
答案 1 :(得分:0)
我的措辞可能不对,但您正在初始化界面中的数字。这样做。
您可以在.m文件中执行此操作(并且不需要@interface evalViewController()@end只显示放置它的位置)
char digits[10] = { ... };
@interface evalViewController ()
@end