0

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
TechChain
  • 8,404
  • 29
  • 103
  • 228
  • Search for "how to create a singleton in Objective-C". Your code is not thread-safe. And do you really want to read a file every time that sharedInstance is called? – gnasher729 Jul 15 '15 at 11:25

1 Answers1

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.

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.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Giuseppe Lanza
  • 3,519
  • 1
  • 18
  • 40