I'm writing a method as below in my View Controller:
- (IBAction)expressionEvaluation:(UIButton *)sender {
NSDictionary *testValues = [NSDictionary dictionaryWithObjectsAndKeys:@"x", 2, @"y", 3, @"z", 4, nil];
//the below line gives the error
double result = [self.brain evaluateExpression:self.brain.expression usingVariableValues:testValues];
NSString *resultString = [NSString stringWithFormat:@"%g", result];
self.display.text = resultString;
}
And in my 'brain' class I have declared the not-yet-finished method:
#import <Foundation/Foundation.h>
@interface CalculatorBrain : NSObject
- (void) pushOperand:(double)operand;
- (void) setVariableAsOperand:(NSString *)variableName;
- (void) performWaitingOperation;
- (double) performOperation:(NSString *)operation;
@property (readonly) id expression;
+ (double)evaluateExpression: (id)anExpression
usingVariableValues: (NSDictionary *)variables; //declared here
@end
...in the .h, and in the .m:
+ (double) evaluateExpression:(id)anExpression usingVariableValues:(NSDictionary *)variables {
double result = 0;
int count = [anExpression count];
for (int i = 0; i < count; i++) {
}
return result;
}
Why am I getting this "No visible @interface for 'CalculatorBrain' declares the selector 'evaluateExpression:usingVariableValues'"error? I'm new to objective-c, but I imagine this means it's not seeing my declaration. I'm not sure if it's a syntax/formatting issue, though, because I'm new to the language.
notice that you declare evaluateExpression:usingVariableValues:
as class method
+ (double)evaluateExpression: (id)anExpression // + means class method
usingVariableValues: (NSDictionary *)variables; //declared here
and use it like instance method
// assuming self.brain is an instance of CalculatorBrain
[self.brain evaluateExpression:self.brain.expression usingVariableValues:testValues];
so either change the method to
- (double)evaluateExpression: (id)anExpression // - means instance method
usingVariableValues: (NSDictionary *)variables; //declared here
or call it like this
[CalculatorBrain evaluateExpression:self.brain.expression usingVariableValues:testValues];
the "+" sign means class method. You can access it through your class name not an instance of it.
like
[MyClass methodName];
a "-" sign means instance method. You can access it through an instance of your class (after having allocated-inited it.
like
MyClass *myInstance = [[MyClass alloc] init];
[myInstance methodName];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With