I want to declare a static int variable in one class and have access to it in every other class. What is the best way to do this?
-
possible duplicate of [iPhone Global Variable?](http://stackoverflow.com/questions/3601341/iphone-global-variable) – jww May 01 '14 at 09:58
4 Answers
There are no static class variables in Objective C. You can create it as a file-scope static variable in the class' implementation file and provide static setter and getter methods in the class.
Or you can make it an old-school global, with an extern
declaration in the .h file. The former approach is more flexible - you can add extra checks in the setter method, for example, the latter is less typing, and avoids the method call overhead.

- 59,826
- 25
- 160
- 281
-
Can I use extern in more than one class to get access to the variable? I have at least 4 classes I want to be able to access it. – Corey Feb 22 '11 at 20:02
-
Sure. Either place the `extern int myvar;` declaration in each file where the variable is needed, or place it into an .h file and #import the .h file where needed. – Seva Alekseyev Feb 22 '11 at 20:35
Here are some ways you could try
Declaring the global variables in appdelegate
Creating a singleton class and putting the global variables there.
Using appdelegate
appdelegate is also a kind of singleton class
Function definition:
-(NSString*)ReadAppDelegateInstanceVariable:(NSString*)InstVar
{
AppDelegate *appDel=(AppDelegate *)[UIApplication sharedApplication].delegate;
return [appDel valueForKey:InstVar];
}
Function Calling:
[self ReadAppDelegateInstanceVariable:@"someInstanceVariableName"];
Using your own singleton class
Only one instance of class can exist.
Sample singleton declaration:
@interface SigletonClass : NSObject
{
//declare instance variable
}
+ (id)sharedSingletonClass;
@end
Sample singleton implementation:
Approach 1: Using GCD
@implementation SigletonClass
+ (id)sharedSingletonClass {
static SigletonClass *sharedClass = nil;
static dispatch_once_t onceToken;//The way we ensure that it’s only created once is by using the dispatch_once method from Grand Central Dispatch (GCD).
dispatch_once(&onceToken, ^{
sharedClass = [[self alloc] init];
});
return sharedClass;
}
- (id)init {
if (self = [super init]) {
//init instance variable
}
return self;
}
@end
Approach 2: Without using GCD
@implementation SigletonClass
+ (id)sharedSingletonClass {
static SigletonClass *sharedClass = nil;
@synchronized(self) {//To safeguard threading issues
if (sharedClass == nil)
sharedClass = [[self alloc] init];
}
return sharedClass;
}
- (id)init {
if (self = [super init]) {
//init instance variable
}
return self;
}
@end
Function definition:
-(NSString*)ReadSingleTonInstanceVariable:(NSString*)InstVar
{
SigletonClass sObj=[SigletonClass sharedSingletonClass];
return [sObj valueForKey:InstVar];
}
Function Calling:
[self ReadSingleTonInstanceVariable:@"SomeInstanceVariableName"];
NSString to int:
-(int)ConvertToIntFromString:(NSString*)str
{
return str.intValue;
}
As far as I’m aware, there are no performance issues with doing it one way over another.
I always prefer singleton class rather than appdelegate because the code will be clutter free and I consider overusing appdelegate as smelly code.

- 31,670
- 10
- 160
- 241
-
the show usage of the singleton is plain wrong. as this code does not enforce a singleton, several calls of `SigletonClass sObj=[[SigletonClass alloc]init];` will create new instances. Also the `sharedSingletonClass` method is using ancient code. Today most prefer `dispatch_once()`. calling `intValue` should be done after some sanity checks, as if it fails it will return 0 — you won't know if it is a valid number or if it failed. – vikingosegundo Dec 08 '13 at 16:05
-
Dear vikingosegundo,The concept of singleton is only one instance should exist.It's a design pattern it can be achieved using variety of ways.Doing it using GCD may be modern But the example I showed here is right.Please visit the below link to verify my answer. https://developer.apple.com/legacy/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32. – Durai Amuthan.H Dec 09 '13 at 06:20
-
1Dear @H.duraiamuthan, and the concept of reputation on SO is that the 25K points I have should tell you, that I know, what the (anti-)pattern Singleton is. But the code you posted is plain wrong. it isn't a singleton. you are creating more than one SingletonClass if you call you `-ReadSingleTonInstanceVariable:` more than once. And please learn the naming convention for cocoa development. – vikingosegundo Dec 09 '13 at 07:35
-
@vikingosegundo Thanks for pointing out.Now I have corrected it.I misunderstood your first comment and pardon my naming convention. – Durai Amuthan.H Jan 21 '14 at 17:26
That breaks some patterns, I'd not use it.
Anyway, if you declare a property in your app delegate then you can call:
[[NSApp delegate] myVar]
anywhere.
How exactly do you intent to use this variable?

- 57,726
- 14
- 108
- 151