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?
int gMoveNumber = 0; at the beginning of your program—outside any method, class definition, or function—its value can be referenced from anywhere in that module. In such a case, we say that gMoveNumber is defined as a global variable.
The C compiler recognizes a variable as global, as opposed to local, because its declaration is located outside the scope of any of the functions making up the program. Of course, a global variable can only be used in an executable statement after it has been declared.
The declaration of a class interface begins with the compiler directive @interface and ends with the directive @end . (All Objective-C directives to the compiler begin with “@”.) // Method and property declarations. The first line of the declaration presents the new class name and links it to its superclass.
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.
Here are some ways you could try
Declaring the global variables in appdelegate
Creating a singleton class and putting the global variables there.
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"];
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With