我正在尝试做非常简单的操作。
在“TestFile.h”文件中我声明了属性:
@property (nonatomic) NSDictionary *justTest;
在实现文件“TestFile.m”中:
-(NSDictionary *)justTest:(NSString *) mystring {
NSLog(@"Here is my string: %@", mystring);
return nil;
现在我正尝试从另一个文件调用“justTest”。我在做什么:
#import "TestFile.h"
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSDictionary *testFile = [[TestFile alloc] init];
[testFile justTest:@"Hello World!"]
}
在我尝试传递参数之前这一切正常。
如果我只是执行
[testFile justTest];
它有效,但是当我尝试传递参数时
[testFile justTest:@"Hello World!"];
不工作,调试消息是:
no visible @interface for 'TestFile' declares the selector 'justTest':
我怎么了?
最佳答案
您需要通过在 @end 之前将方法名称添加到 TestFile.h 文件来公开此方法:
-(NSDictionary *)justTest:(NSString *) mystring;
只是为了让你知道当你添加@property 编译器合成它(创建)两个方法 getter,与你的属性和 setter 完全相同的名称编译器添加'set'前缀,例如,你声明:
@property (nonatomic) NSDictionary *justTest;
编译器会创建两个方法:
-(NSDictionary *)justTest {...}
-(void)setJustTest {...}
您需要知道在您的代码中您覆盖了 getter 方法。
关于ios - 如何将参数传递给外部类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23632539/