Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6611931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:02:06+00:00 2026-05-25T20:02:06+00:00

After reading the responses to a question about singletons in Objective C it appears

  • 0

After reading the responses to a question about singletons in Objective C it appears that each solution makes some tradeoff in regards to threading in the instance accessor. i.e.

@synchronized(self)
{
    if (sharedInstance == nil)
            sharedInstance = [[MySingleton alloc] init];
}
return sharedInstance;

This essentially single-threads access to the singleton, and if it’s something that’s used frequently in an operation, seems like something that could cause threads to unnecessarily contend.

What’s the downside to simply using the class object as the singleton instance, and exposing functionality via class methods, i.e.

@interface MySingleton : NSObject {
}

+ (void)doSomething;
@end

@implementation MySingleton    
+ (void)initialize {
   //do some setup if necessary
}

+ (void)doSomething {
    //do something
}
@end

In this way we avoid doing the lock + check every time we want to reference the singleton object and we can also eliminate having to store it in a local or method ivar.

This approach also lets the runtime guarantee that only one instance (the Class object) exists in the system at any given time.

EDIT

There’s more here than just threading, with a traditional singleton you usually write code like this:

MySingleton *instance = [MySingleton getSharedInstance];
NSObject *someResult = [instance getResult];
//or
if (instance.someProperty) {
  //do something
}

However if your singleton is a class instance, you essentially eliminate the need call getSharedInstance all the time. Consider this code:

NSObject *someResult = [MySingleton getResult];
//or
if ([MySingleton someProperty]) {
  //do something
}

I hear the point that you have to store your data in file local static variables, or in global variables (yuck). But it’s really not all that different from a traditional singleton with the exception that you lose Objective-C 2.0 properties (instead you have to use traditional accessor methods).

Here’s one key tradeoff for me that seems like a win. In a traditional singleton you end up overriding -copyWithZone, +allocWithZone, -retain, -retainCount, -release and -autorelease if you really want to get things right.

This seems like an awful lot of work to do every time you want to write a simple Singleton object (they happen to be pretty useful). So why not simply just replace it with this:

@implementation MySingleton
+ (void)initialize {
    //do your setup
}

- (id)init {
    NSAssert(NO, @"You should read the documentation on singletons.");
}
@end

It’s a lot lighter in terms of code, and unless your consumers are really sneaky they won’t ever create two instances.

Get to the point already
My question is really this:

Is there any drawback to using the Class object as the instance of your singleton?

It seems like you can take all the same steps in terms of threadsafety, memory efficiency etc. without having to remember to override so many methods and accessors or litter your code with instance checks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:02:07+00:00Added an answer on May 25, 2026 at 8:02 pm

    This is my first post on Stack Overflow… (so prepare for stupidity)

    I think there is a hybrid solution that might be useful.

    I want to set and get (global) values out of a singleton class without having calling “getSharedInstance”. I’d want the code to look like this…

    frameRate = Singleton.frameRate;
    Singleton.frameRate = 42;
    

    To achieve this, each variable we need to store in the singleton has a getter and setter class method. The class method then goes to an instance to store the data in an ivar. The instance isn’t directly accessed by the main program.

    The getter looks like this:

    + (int) frameRate
    {
        return [[Singleton instance] ivarFrameRate];
    }
    

    The (ugly) instance call is hidden inside the class code.

    By calling the instance method here, the class method will automatically instantiate an object when first used. Once the singleton is instantiated, the instance stores ivars conventionally. Here, I am prefixing with “ivar” make the ivar explicit.

    @property  int ivarFrameRate;
    

    and

    @synthesize ivarFrameRate;
    

    This automatically creates conventional getter (and setter) methods to access the ivar.

    (edit – here is a complete example)

    //  Singleton.h
    #import <Foundation/Foundation.h>
    @interface Singleton : NSObject
    {
    float ivarFrameRate
    }
    
    @property  float ivarFrameRate;
    
    - (id) init;
    + (Singleton *) instance;
    + (float) frameRate;
    + (void) setFrameRate:(float)fr;
    @end
    

    and

    //  Singleton.m
    #import "Singleton.h"
    @implementation Singleton
    @synthesize ivarFrameRate;
    
    static Singleton* gInstance = NULL;
    
    + (Singleton*)instance
    {
        @synchronized(self)
        {
            if (gInstance == NULL)
            gInstance = [[self alloc] init];
        }
        return(gInstance);
    }
    
    
    - (id)init
    {
        self = [super init];
        return self;
    }
    
    + (float) frameRate
    {
        return [[Singleton instance] ivarFrameRate];
    }
    
    + (void) setFrameRate:(float)fr;
    {
        [[Singleton instance] setIvarFrameRate:fr];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this question , I was reminded of when I was taught Java
After reading a bit more about how Gnutella and other P2P networks function, I
After reading Practical Common Lisp I finally understood what the big deal about macros
After reading the answers to the question Calculate Code Metrics I installed the tool
After reading the Test-and-Set Wikipedia entry , I am still left with the question
I have a question with fluent interfaces. We have some objects that are used
After reading a StackoOverflow question https://stackoverflow.com/questions/182112/funny-loading-statements-to-keep-users-amused , I was really intrigued to ponder upon
After playing with Node.js and reading about async i/o & evented programming a lot
After reading the Head First Design Patterns book and using a number of other
After reading this description of late static binding (LSB) I see pretty clearly what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.