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 6928249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:09:43+00:00 2026-05-27T11:09:43+00:00

Assume you have a Singleton Constants class, instance of which you’d like to use

  • 0

Assume you have a Singleton Constants class, instance of which you’d like to use throughout your application.

In someClass, therefore we can reference [Constants instance] someCleverConstant];

Typing this gets old really quick and it would be nice to get a shortcut to the instance.

  • In someClass, we can declare @property (nonatomic, weak, readonly) Constants *constants;
  • And a getter to the instance
-(Constants*) constants {
  if (constants == nil) 
    constants = [Constants instance];
  return constants;
}

This way in someClass, therefore we can reference constants.someCleverConstant; instead

A few questions on this:

  • Is what i described a reasonable approach?
  • Is it correct to declare a property weak?
  • Is there any performance concerns with what i have described? Would it actually be better to call instance directly?
  • Consider a situation where you have 20 classes, each needing it’s own pointer to Constants instance. Would this approach work then?

Thank you for your time.

  • 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-27T11:09:44+00:00Added an answer on May 27, 2026 at 11:09 am

    You could just create a global pointer to your singleton, like NSApp for [NSApplication sharedApplication].

    Presumably you’ve already got something like

    static Constants * defaultInstance = nil;
    

    at the top of your implementation file. If you remove the static, and declare the variable in your header (keeping the definition in the .m file):

    @interface Constants : NSObject
    // etc.
    @end
    
    extern Constants * defaultInstance;
    

    You can then access the singleton instance via the name defaultInstance (probably want to change that name, though) in any file that imports the header (which you must be doing anyways). You’ll have to call your singleton setup method (+instance or whatever) somewhere very early in your program, such as -applicationDidFinishLaunching to be sure that the pointer is set before you use it.

    • Is what I described a reasonable approach?

    I think there are other, better approaches, described above and in Paul.s’s answer.

    • Is it correct to declare a property weak?

    Yes, the class that has this pointer doesn’t need to own it, because the singleton owns itself;

    • Is there any performance concerns with what i have described? Would it actually be better to call instance directly?

    Either way, [Constants instance] or self.constants you’re doing a message send. The first time you do self.constants, you’re doing two. None of this should be a real concern, though.

    • Consider a situation where you have 20 classes, each needing it’s own pointer to Constants instance. Would this approach work then?

    To me, it seems unwieldy and inelegant.

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

Sidebar

Related Questions

Assume I have a singleton class in an external lib to my application. But
Assume I have a class foo, and wish to use a std::map to store
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
I have a singleton class, whose instance get initialized at global scope within the
Assume we have a Menu class that has SubMenus (which is the same type
Assume I have a Class Foo which has many internal variables, only one constructor
Assume you have some objects which have several fields they can be compared by:
Assume I have a form class SampleClass(forms.Form): name = forms.CharField(max_length=30) age = forms.IntegerField() django_hacker
Assume we have a method like this: public IEnumerable<T> FirstMethod() { var entities =
Assume you have five products, and all of them use one or more of

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.