In my .m file for a class named Ad , I have 3 static strings
static NSString *AdStateDisabled = @"disable";
static NSString *AdStateExpired = @"expired";
static NSString *AdStateActive = @"active";
I can simply use these static variables in the current class, but i cannot call them from any other class, is there a way to make these static variables global? So for example in my viewcontroller class i can do something like.
//HomeViewController.m
if ([self.ad.state isEqual:Ad.AdStateDisabled])
{
//do something
}
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.
"In both C and Objective-C, a static variable is a variable that is allocated for the entire lifetime of a program. This is in contrast to automatic variables, whose lifetime exists during a single function call; and dynamically-allocated variables like objects, which can be released from memory when no longer used.
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.
Keyword extern is used for declaring extern variables in Objective-C. This modifier is used with all data types like int, float, double, array, pointer, function etc. Basically extern keyword extends the visibility of the C variables and C functions. Probably that's is the reason why it was named as extern.
First, remove the static. Static variables and functions in C and Objective-C mean that they're visible only to the current compilation unit (that is more or less: only the file that you've declared the statix variable in can see it).
Next, you also need to declare the variables in a public header file with "extern", like the one of the class associated with the class:
extern NSString *AdStateDisabled;
You can then use them in other files as well, but you would not access them as "Ad.AdStateDisabled" but just as "AdStateDisabled".
You could add the following declarations to your HomeViewController.h header, which would then need to be imported anywhere you wanted access to the strings.
//HomeViewController.h
extern NSString *AdStateDisabled;
extern NSString *AdStateExpired;
extern NSString *AdStateActive;
Then alter your definitions to remove 'static'.
//HomeViewController.m
NSString *AdStateDisabled = @"disable";
NSString *AdStateExpired = @"expired";
NSString *AdStateActive = @"active";
If you don't want a user of the strings to have to import HomeViewController.h then you could also just define those strings in AdState.h and put the definitions into AdState.m (and remove them from HomeViewController.m) after which users of the string would just need to import AdState.h to use the strings.
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