views:

35

answers:

1

i am following along(mostly, im playing around with different functionality) with sams teach yourself cocoa touch programming and im building a calculator but when i try to initialize an object of the calculatormodel class i get this error: " error: initializer element is not constant". can anyone explain this or give me a solution? thanks so much

#import "CalculatorController.h"
#import "CalculatorModel.h";



@implementation CalculatorController

CalculatorModel *calc = [[CalculatorModel alloc] init]; //error is here

-(void) pressButton:(UIButton*) sender;{

if ([[(UIButton *)sender currentTitle] isEqualToString:@"0"]){
    [calc setValue:0];
    NSLog(@"Value: %i\n", calc.value);
}
else if ([[(UIButton *)sender currentTitle] isEqualToString:@"1"]){
    [calc setValue:1];
    NSLog(@"Value: %i\n", calc.value);
}   
}

- (id) init {
    fprintf(stderr, "CalculatorController created");
    return [super init];
}
@end
+1  A: 

The issue is you're initializing a file level object, move CalculatorModel *calc = [[CalculatorModel alloc] init]; into the init method.

Joshua Weinberg
okay i moved that statement to the init method now i get a different error saying 'calc' is undeclared. shouldn't it be declared when the init method is run...do i need to put the init method above where i want to use calc?
Joe
calc needs to become an instance variable.
Joshua Weinberg