ios - 'class-name' 没有可见的@interface 声明选择器 'method-name'

标签 ios objective-c syntax

我正在我的 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;
}

在我的“大脑”类(class)中,我声明了尚未完成的方法:

#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

...在 .h 和 .m 中:

+ (double) evaluateExpression:(id)anExpression usingVariableValues:(NSDictionary *)variables {
    double result = 0;
    int count = [anExpression count];
    for (int i = 0; i < count; i++) {

    }

    return result;
}

为什么我会收到“'CalculatorBrain' 的不可见@interface 声明选择器'evaluateExpression:usingVariableValues'”错误?我是 objective-c 的新手,但我想这意味着它没有看到我的声明。不过,我不确定这是否是语法/格式问题,因为我是该语言的新手。

最佳答案

请注意,您将 evaluateExpression:usingVariableValues: 声明为类方法

+ (double)evaluateExpression: (id)anExpression  // + means class method
     usingVariableValues: (NSDictionary *)variables; //declared here

像实例方法一样使用它

// assuming self.brain is an instance of CalculatorBrain
[self.brain evaluateExpression:self.brain.expression usingVariableValues:testValues];

所以要么把方法改成

- (double)evaluateExpression: (id)anExpression  // - means instance method
     usingVariableValues: (NSDictionary *)variables; //declared here

或者这样称呼

[CalculatorBrain evaluateExpression:self.brain.expression usingVariableValues:testValues];

关于ios - 'class-name' 没有可见的@interface 声明选择器 'method-name',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15058475/

相关文章:

objective-c - 如何使用graph api、objective c将本地镜像发布到用户的 friend 墙上?

python - 为什么 `for t[1] in iterable` 是有效语法?

ios - 将图像从 ios 应用程序上传到服务器

objective-c : Twitter login error on iPad : Whoa there

iphone - 在 iOS 中删除 html 标签和不需要的字符

变量声明中的 MySQL 语法错误

html - 正确的 HTML 标记语法? (删除行内 block 元素之间的空格)

ios - UITableView 显示静态内容

android - 如何在 Xamarin iOS/Android 中使用 Azure 媒体服务

ios - 如何在 View 中切出具有径向渐变的孔