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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:33:43+00:00 2026-05-16T15:33:43+00:00

which is the cleanest way to use something like a global variable? Normally, using

  • 0

which is the cleanest way to use something like a global variable? Normally, using a global variable is forbidden, but I don’t know a better solution for accessing NSUserDefaults from different classes.

I read a bit and come up with this. I define a Contants.h and a Constants.m file and include them everywhere I need to.

 //Constants.h
 #import <Foundation/Foundation.h>


 @interface Constants : NSObject {
  extern NSUserDefaults *settings;
 }

 @end

.

 //Constants.m
 @implementation Constants

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"];
 NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
 [[NSUserDefaults standardUserDefaults] registerDefaults:settingsDict];
 NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];

 @end

The problem here is that I want to initialize a value to my constant. I have no method in Constants.m. So my helper variables would also be globals?

One thing to mention: I think the global variable also has to be released?

Thanks for your help!

Edit:

@hotpaw2:

AppBundleSingleton.h:

#import <Foundation/Foundation.h>


@interface AppBundleSingleton : NSObject {

}

+ (AppBundleSingleton *)sharedAppBundleSingleton;

@end

AppBundleSingleton.m:

#import "AppBundleSingleton.h"


static AppBundleSingleton *sharedAppBundleSingleton = nil;


@implementation AppBundleSingleton

#pragma mark -
#pragma mark Singleton methods

+ (AppBundleSingleton *)sharedAppBundleSingleton {
    @synchronized(self) {
        if (sharedAppBundleSingleton == nil) {
            sharedAppBundleSingleton = [[self alloc] init];
        }
    }
    return sharedAppBundleSingleton;
}

+ (id)allocWithZone:(NSZone *)zone {
    @synchronized(self) {
        if (sharedAppBundleSingleton == nil) {
            sharedAppBundleSingleton = [super allocWithZone:zone];
            return sharedAppBundleSingleton;  // assignment and return on first allocation
        }
    }
    return nil;  // on subsequent allocation attempts return nil
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

- (id)retain {
    return self;
}

- (NSUInteger)retainCount {
    return NSUIntegerMax;  //denotes an object that cannot be released
}

- (void)release {
    //do nothing
}

- (id)autorelease {
    return self;
}

-(id)init {
    self = [super init];
    sharedAppBundleSingleton = self;
    // Initialization code here
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"];
    NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
    [[NSUserDefaults standardUserDefaults] registerDefaults:settingsDict];

    return self;
}

@end

In my AppDelegate.m I have the following:

// ...
#include "AppBundleSingleton.h"


@implementation MyAppDelegate

// ...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Add the navigation controller's view to the window and display.
    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    [AppBundleSingleton sharedAppBundleSingleton];

    return YES;
}
// ...
@end

In my ViewController I query the values:

NSString *myString = [[NSUserDefaults standardUserDefaults] stringForKey:@"myKeyforString"];

Would that be a solution?

  • 1 1 Answer
  • 2 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-16T15:33:44+00:00Added an answer on May 16, 2026 at 3:33 pm

    Global variables are not only NOT forbidden, but a required part of the ANSI C specification, which is a subset of Obj C. Both gcc and llvm fully support globals. They are often the smallest and fastest way to pass unprotected values around.

    That said, explicit use of global variables are most probably not the solution to your problem. You have a problem quite well suited to the MVC paradigm. Place all your NSDefault code into a singleton model class (an M of the MVC), which can self-initialize on first access (the implementation may use a hidden global), and attach that object to the appDelegate where it can easily be obtained from anywhere. Then encapsulate all you default value read/writes as properties of that object.

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

Sidebar

Related Questions

What's the cleanest way to add a prefix to every URL in CakePHP, like
I'd like to create a webpage which looks like this: https://i.stack.imgur.com/GYEis.png I can't use
Which is more better , powerful and flexible between Graphiti or Draw2D, As i
Which library has the definition for the global new and the delete operator? Specifically
Which is better? 1. = link_to Page, /page, :class => button, :data => {:theme
Which is the quickest way to check the presence of Struts runtime from a
Which are the best methods for implementing replication on servers using Java RMI. I
I have some code to interface Python to C++ which works fine but every
Looking for suggestions on cleanest way to get the return value and the result
What would be the cleanest way to have a Save state for an application

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.