I am having a class in which i am using singleton pattern.I have static method.Now i want call a non static method inside the static method.But i am not able to call it.Please tell me what is the solution.
我正在使用单例模式的类。我有静态方法。现在我想在静态方法中调用非静态方法。但是我无法调用它。请告诉我解决方法是什么。
#import "ThemeManager.h"
@implementation ThemeManager
+(ThemeManager *)sharedInstance
{
NSLog(@"shared instance called");
static ThemeManager *sharedInstance = nil;
if (sharedInstance == nil)
{
sharedInstance = [[ThemeManager alloc] init];
}
[self getPref];//i get error at this line
return sharedInstance;
}
-(void)getPref
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *themeName = [defaults objectForKey:@"theme"] ?: @"default";
NSLog(@"theme name is %@",themeName);
NSString *path = [[NSBundle mainBundle] pathForResource:themeName ofType:@"plist"];
self.theme = [NSDictionary dictionaryWithContentsOfFile:path];
}
@end
1 个解决方案
#1
6
[sharedInstance getPref]
non static methods are instance methods. the receiver must be an instance. in your case, the instance you want to use is of course the sharedInstance that you just created.
非静态方法是实例方法。接收者必须是一个实例。在您的情况下,您要使用的实例当然是您刚刚创建的sharedInstance。
inside a class method self is the class herself. that's why you get an error on that line, because self is not an instance in that context.
在一个类方法中,self是类本身。这就是你在该行上出错的原因,因为self不是该上下文中的实例。
#1
6
[sharedInstance getPref]
non static methods are instance methods. the receiver must be an instance. in your case, the instance you want to use is of course the sharedInstance that you just created.
非静态方法是实例方法。接收者必须是一个实例。在您的情况下,您要使用的实例当然是您刚刚创建的sharedInstance。
inside a class method self is the class herself. that's why you get an error on that line, because self is not an instance in that context.
在一个类方法中,self是类本身。这就是你在该行上出错的原因,因为self不是该上下文中的实例。