如何从目标c中的静态方法调用非静态方法?

我有一个类,我正在使用单例模式。我有静态方法。现在我想调用静态方法内的非静态方法,但我无法调用它。请告诉我什么是解决scheme。

#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 

 [sharedInstance getPref] 

非静态方法是实例方法。 接收者必须是一个实例。 在你的情况下,你想要使用的实例当然是你刚创build的sharedInstance。

在class级方法里面,自己就是class级。 这就是为什么你会在这一行上得到一个错误,因为self不是这个上下文中的一个实例。