Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use static variable(BOOL) in Objective C

I am from C# background, and I am having a hard time in figuring out about how to use a static variable(BOOL in my case) in Objective C. My questions are:

  1. Where should I declare my static variable.
  2. How can I access(set its value) from another class.
  3. Do I need to use extern keyword.
like image 897
Xavi Valero Avatar asked Mar 25 '12 09:03

Xavi Valero


1 Answers

Declare static variable in your implementation file and provide class method to set/get vlaue of it.

// MyClass.h
@interface MyClass : NSObject {
}
+ (BOOL)gBoolean;
+ (void)setGBoolean:(BOOL)value;
@end

// MyClass.m
#import "MyClass.h"

static BOOL gBoolean;

@implementation MyClass

+ (BOOL)gBoolean; {
    return gBoolean;
}

+ (void)setGBoolean:(BOOL)value; {
gBoolean = value;
}
@end

Take a look at this answer.

like image 84
Parag Bafna Avatar answered Nov 14 '22 15:11

Parag Bafna