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

Ios 如何从目标c中的静态方法调用非静态方法?,ios,objective-c,iphone,ipad,oop,Ios,Objective C,Iphone,Ipad,Oop,我有一个使用单例模式的类。我有静态方法。现在我想在静态方法中调用一个非静态方法。但是我不能调用它。请告诉我解决方案是什么 #import "ThemeManager.h" @implementation ThemeManager +(ThemeManager *)sharedInstance { NSLog(@"shared instance called"); static ThemeManager *sharedInstance = nil; if (share

我有一个使用单例模式的类。我有静态方法。现在我想在静态方法中调用一个非静态方法。但是我不能调用它。请告诉我解决方案是什么

#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

在类方法内部,self是类本身。这就是为什么在这一行中会出现错误,因为self在该上下文中不是一个实例

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

在类方法内部,self是类本身。这就是为什么在这一行中会出现错误,因为self不是该上下文中的实例。

搜索“如何在Objective-C中创建单例”。您的代码不是线程安全的。你真的想在每次调用sharedInstance时都读取一个文件吗?搜索“如何在Objective-C中创建单例”。您的代码不是线程安全的。您真的想在每次调用sharedInstance时读取文件吗?
[sharedInstance getPref]