6

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.
Xavi Valero
  • 2,047
  • 7
  • 42
  • 80

1 Answers1

8

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.

Community
  • 1
  • 1
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144