0

I am very new to Objective-C. I need to make a global variable. I have the files abc.h, abc.m, aaa.h, aaa.m and of course, the app delegate.

I want to declare it in abc.h, use have the user assign it in abc.m and it be used in aaa.m. I want the variable to be an integer named x. I heard that I can use the App Delegate Somehow. I want the assigning variable in abc.m to be implemented in the middle of my code. Since I'm new, please make it simple!!

Thanks in Advance!

Shawn
  • 245
  • 2
  • 3
  • 7
  • 1
    See: http://stackoverflow.com/questions/1113980/global-variables-in-objective-c – jmnwong Sep 10 '11 at 00:36
  • possible duplicate of [In Objective-C, how do you declare/use a global variable?](http://stackoverflow.com/questions/5098990/in-objective-c-how-do-you-declare-use-a-global-variable) or [Global variables in Xcode](http://stackoverflow.com/questions/7081245/global-variables-in-xcode) or [Objective-C static and global variable](http://stackoverflow.com/questions/3965347/objective-c-static-and-global-variable) or [iPhone Global variable](http://stackoverflow.com/questions/3601341/iphone-global-variable) – jscs Sep 10 '11 at 02:47
  • The following answer in the link clearly explains http://stackoverflow.com/a/20423815/730807 – Durai Amuthan.H Jun 28 '14 at 09:06

3 Answers3

4

You can use a property in your application delegate, as you can always get the app delegate instance with:

[ [ UIApplication sharedApplication ] delegate ]

So:

/* AppDelegate.h */
@interface AppDelegate: NSObject < UIApplicationDelegate >
{
    int x;
}
@property( readonly ) int x;
@end

/* AppDelegate.m */
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize x;
@end

This way, you'll be able to use:

[ [ [ UIApplication sharedApplication ] delegate ] x ]

Another approach is to use a global variable, declared as extern in your abc.h file, and defined in the abc.m file.

/* abc.h */
extern int x;

/* abc.m */
int x = 0;

This way, other files will be able to access x, only by including abc.h. extern tells the compiler that the variable will be defined later (e.g. in another file), and that it will be resolved at link time.

Macmade
  • 52,708
  • 13
  • 106
  • 123
  • Thanks MacMade, what does (readonly) mean and also, how can I use [ [ [ UIApplication sharedApplication ] delegate ] x ]? And how do I include abc.h? – Shawn Sep 10 '11 at 01:55
  • If you don't know that, then you should really take a look at the basic Objective-C docs... – Macmade Sep 10 '11 at 13:58
1

Instead of putting all the burden in AppDelegate I'll recommend you to create your own singleton class, and then use it anywhere you want to use. Here is the sample of creation of singleton class: http://www.71squared.com/2009/05/iphone-game-programming-tutorial-7-singleton-class/

UPT
  • 1,490
  • 9
  • 25
0

I'd recommend creating your own singleton class so as to avoid cluttering up the UIApplication delegate. It also makes your code neater. Subclass NSObject and add code a bit like the following:

static Formats *_formatsSingleton = nil;
+ (Formats*) shared
{
    if (_formatsSingleton == nil)
    {
        _formatsSingleton = [[Formats alloc] init];
    }
    return _formatsSingleton;
}

Add ivars and properties to this class as required. You can set default values in an init method, e.g.

- (id) init;
{
    if ((self = [super init]))
    {
        _pgGlobalIntA = 42;   // ivar for an int property called globalInt
        _pgGlobalStringB = @"hey there";   // ivar for an NSString property called globalStringB
    }
    return self;
}

And then to set and access you'd use:

 [[Formats shared] setGlobalIntA: 56];
 NSLog(@"Global string: '%@'", [[Formats shared] globalStringB]);

The class method shared creates an instance of the class if one doesn't exist already. So you don't have to worry about creating it. It'll just happen the first time you try to access or set one of your globals.

Obliquely
  • 7,002
  • 2
  • 32
  • 51