我在从 View Controller 调用类方法时遇到问题。类方法将 BOOL 作为输入。 Xcode 允许我在没有参数的情况下调用它(这显然会崩溃),但当我传递参数时会提示。
相关代码如下:
// RLI.h
@interface RLI : NSObject <CLLocationManagerDelegate>
...
+(BOOL)askForLocationAccess;
end
// RLI.m
@implementation RLI
...
+(BOOL)askForLocationAccess:(BOOL)ask {
...
return answer;
}
end
// CLOnboardingViewController.m
@implementation CLOnboardingViewController
...
-(IBAction)allowGPSAccess:(id)sender {
// NSLog(@"%@", [sender currentTitle]);
BOOL ask = [[sender currentTitle] isEqualToString:gpsLabel];
[RLI askForLocationAccess:YES]; // No known class method for selector 'askForLocationAccess:'
[RLI askForLocationAccess]; // no complaint but crashes with "unrecognized selector sent to a class"
[RLI askForLocationAccess:ask]; // what I intend to do
...
}
end
如果需要更多代码,请告诉我,但这似乎应该足够简单。我查看了一堆“没有已知的选择器类方法”问题,但其中大部分是错别字或调用实例方法。
最佳答案
我认为这是因为你的头类有没有参数的方法。 这是您的 .h 文件中的内容:
+(BOOL)askForLocationAccess;
将其更改为:
+(BOOL)askForLocationAccess:(BOOL)ask;
关于ios - 不能调用带参数的类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24271061/