0

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 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
calc needs to become an instance variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.