-2

I'm trying to create a singleton. This group of code is meant to establish a settingsMAnager singleton.

Im trying to allocate it but keep throwing an error

Its throwing the error no visible @ interface with NSObject. Declares the selector 'alloc'

Can someone see what's wrong?

Thanks in advance!

//In my .h file I have

    +(settingsManager*)getInstance;
    -(void)printSettings;


//In My .m file is----

    static settingsManager *theInstance = nil;

//Instance Method
    +(settingsManager*)getInstance
    {


         (theInstance == nil)

        {
            [[self alloc] init];//Im getting "expression result unused" here
        }

    return theInstance;

    }

    -(id)alloc

    {
        theInstance = [super alloc];<------//getting the big error here 


    return theInstance;

    }

    -(id)init
    {

    if (self = [super init])

         {

    }
    return  self;


}
(void)printSettings                                                                                                                                                                                   


{



    NSLog(@"Hello");




}

2 Answers2

3

You should never subclass the alloc method. Below is the code to use a singleton:

+ (instancetype)sharedInstance {
    
    static SettingsManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[settingsManager alloc] init];
    });
    return sharedInstance;
}

And if you're interested in some more reading, I suggest you this link.

I'd also suggest you to read the recommended coding guidelines for objective-c.



instancetype

instancetype is a contextual keyword that can be used as a result type to signal that a method returns a related result type. instancetype, unlike id, can only be used as the result type in a method declaration. More details here.

dispatch_once

As explained here, dispatch_once() is synchronous and allows you to execute a piece of code only once.

Community
  • 1
  • 1
jbouaziz
  • 1,484
  • 1
  • 12
  • 24
  • Thank you jbouaziz....I thank you for your advice and informative reading. I will look at both! – Howie Livingston Mar 17 '14 at 18:18
  • jbouaziz...could you explain to me what shappening on line four specifically? The code works....are you creating a token? Do I need to use this later on? I will need to use instanceType and sharedInstance and I know how...Just unfamiliar with that syntax. Could you elaborate for someone that just got an answer to a difficult problem? – Howie Livingston Mar 17 '14 at 18:42
0

This is not the right way to implement a singleton. There are a number of unidiomatic and potentially risky bits in your implementation. However, this error is coming from the fact that alloc is a class method, not an instance method as you've written it here.

Community
  • 1
  • 1
Chuck
  • 234,037
  • 30
  • 302
  • 389
  • I want to thank everyone for their help....I'm not to proud to say I'm new...I wanted to be a computer person all of my life. I'm finally learning and am glad to find a community of knowledgable people to turn too for direction. I WILL pay more attention to the guidelines of the site, like marking someones answer helpful..my apologies....I've only made four posts in a year and a half so..thanks for everyone's help! – Howie Livingston Mar 25 '14 at 21:13