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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.