Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global int variable objective c

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?

like image 542
Corey Avatar asked Feb 22 '11 16:02

Corey


People also ask

How do you declare a global variable in Objective-C?

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.

Can you declare global variables in C?

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.

How do you declare a class variable in Objective-C?

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.


3 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.

like image 133
Seva Alekseyev Avatar answered Oct 13 '22 01:10

Seva Alekseyev


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.

like image 28
Durai Amuthan.H Avatar answered Oct 13 '22 00:10

Durai Amuthan.H


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?

like image 43
sidyll Avatar answered Oct 12 '22 23:10

sidyll