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

  • SEARCH
  • Home
  • 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 6199101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:07:51+00:00 2026-05-24T04:07:51+00:00

Good day, friends. Once again stupid question about Obj-C from newbie :) I’m trying

  • 0

Good day, friends.

Once again stupid question about Obj-C from newbie 🙂

I’m trying to implement singleton design pattern in Obj-C:

@interface SampleSingleton : NSObject {
@private
    static SampleSingleton* instance;
}
+(SampleSingleton*) getInstance;

Compiler returns error: “expected specifier-qualifier-list before ‘static'”.

  • 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-24T04:07:52+00:00Added an answer on May 24, 2026 at 4:07 am

    Please find below the Objective-C code snippet I am using, for proper thread-safe singleton implementation

    header file :

    /*
     *
     * Singleton interface that match Cocoa recommendation
     * @ http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32
     * extended with thread-safe pattern
     */
    @interface MyCustomManager : NSObject { 
    }
    
    #pragma mark Singleton Thred-Safe Pattern
    
    + (MyCustomManager *) sharedInstance;
    + (id)allocWithZone:(NSZone *)zone;
    - (id)copyWithZone:(NSZone *)zone;
    - (id)retain;
    - (NSUInteger)retainCount;
    - (void)release;
    - (id)autorelease;
    
    #pragma mark -
    

    implementation file :

    /*
     * My custom manager Class singleton implementation
     */
    @implementation MyCustomManager
    
    #pragma mark Initializers
    
    /*
     * specific initialize goes here
     */
    - (void) specificInitialize
    {
        // ...
    }
    
    /*
     * Ensure any owned object is properly released
     */
    - (void) dealloc
    {
    [super dealloc];
    }
    
    #pragma mark -
    
    #pragma mark Singleton Thred-Safe Pattern
    
    //- use Volatile to make sure we are not foiled by CPU caches
    static void * volatile sharedInstance = nil;                                                
    
    /*
     * retrieve sharedInstance based on OSAtomicCompareAndSwapPtrBarrier that 
     * acts as both a write barrier for the setting thread and a read barrier from the testing thread
     * more info @ http://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like/2449664#2449664
     * and http://stackoverflow.com/questions/6915/thread-safe-lazy-contruction-of-a-singleton-in-c/6943#6943
     */
    + (MyCustomManager *) sharedInstance {  
        //- check sharedInstance existenz 
        while (!sharedInstance) {  
            //- create a temporary instance of the singleton    
            id temp = [super allocWithZone:NSDefaultMallocZone()];
            //- The OSAtomicCompareAndSwapPtrBarrier function provided on Mac OS X 
            //- checks whether sharedInstance is NULL and only actually sets it to temp to it if it is. 
            //- This uses hardware support to really, literally only perform the swap once and tell whether it happened.
            if(OSAtomicCompareAndSwapPtrBarrier(0x0, (void *)temp, &sharedInstance)) {
                //- compute singleton initialize
            MyCustomManager *singleton = (MyCustomManager *) sharedInstance;
                [singleton specificInitialize];
            }
            else {
                //- if the swap didn't take place, delete the temporary instance
                [temp release]; 
                temp = nil;
            }                                                                                                 
        }   
        //- return computed sharedInstance
        return sharedInstance;                                                                        
    }
    
    /*
     * method to ensure that another instance is not allocated if someone tries to allocate 
     * and initialize an instance of your class directly instead of using the class factory method. 
     * Instead, it just returns the shared object.
     */
    + (id)allocWithZone:(NSZone *)zone
    {
        return [[self sharedInstance] retain];
    }
    
    /*
     * Implements the base protocol methods to do the appropriate things to ensure singleton     status. 
     * Applies to memory-managed code, not to garbage-collected code
     */
    - (id)copyWithZone:(NSZone *)zone
    {
        return self;
    }
    
    /*
     * Implements the base protocol methods to do the appropriate things to ensure singleton status. 
     * Applies to memory-managed code, not to garbage-collected code
     */
    - (id)retain
    {
        return self;
    }
    
    /*
     * Implements the base protocol methods to do the appropriate things to ensure singleton status. 
     * Applies to memory-managed code, not to garbage-collected code
     */
    - (NSUInteger)retainCount
    {
        return NSUIntegerMax;  //denotes an object that cannot be released
    }
    
    /*
     * Implements the base protocol methods to do the appropriate things to ensure singleton status. 
     * Applies to memory-managed code, not to garbage-collected code
     */
    - (void)release
    {
        //do nothing
    }
    
    /*
     * Implements the base protocol methods to do the appropriate things to ensure singleton status. 
     * Applies to memory-managed code, not to garbage-collected code
     */
    - (id)autorelease
    {
        return self;
    }
    
    #pragma mark -
    

    Just to help you starting in objective-c and not get lost in your project structure, you can consider having the project structure matching your file system so as your project becomes bigger you won’t get lost.

    Also please consider using a proper class naming convention, and stick to it.

    I a providing you mine as sample:

    • Any class that match singleton pattern is named using Manager suffix (E.g. MyCustomManager ).

    • Any static class is named using Helper suffix (E.g. MyCustomHelper).

    • 
Any class dedicated to control particular process is named using Controller suffix ( E.g. MyParticularTaskConstroller ).


    • Any UI control that inherit from another control needs provide control suffix ( E.g. MyCustomDetailCell inheriting from UITableViewCell )

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day, I have a question here. I am trying to fetch image from
Good day everyone. I have a question about making and using derived classes of
Good day, friends. I'm newbie in Objective-C. I'm wanting to use enum in my
Good day, friends. I have a PreferenceActivity, it is filled from XML file. When
Good day friends! I'm experiencing one huge problem here! First, I posted a question
Good day, just a quick question: I would like to bind a table to
Good day, I try to start new Activity from another. But it always crashed.
Good day, all. I know that this is a pretty basic question in terms
Good day, I am having an issue trying to get the Text on a
Good day. I'm trying to filter logs with get-winevent. When I working with local

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.