Reputation: 3120
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.
Upvotes: 4
Views: 4584
Reputation: 2080
MOXY is right.
+ (double)evaluateExpression: (id)anExpression;
is a class method, and is sent to the class. You typically use class methods for things like constructing new objects:
+ (NSString*) stringWithString: (NSString*) s;
What you want is an instance method:
- (double) evaluateExpression: (id) anExpression;
which you would send to an instance on your object.
Upvotes: 2
Reputation: 46608
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];
Upvotes: 4
Reputation: 1624
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];
Upvotes: 3