Is goal C a static and global variable?

In my .m file for a class called Ad, I have 3 static lines

static NSString *AdStateDisabled = @"disable"; static NSString *AdStateExpired = @"expired"; static NSString *AdStateActive = @"active"; 

I can just use these static variables in the current class, but I cannot name them from any other class, is there any 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 } 
+7
source share
2 answers

The following declarations can be added to the HomeViewController.h header, which should then be imported anywhere you need access to the strings.

 //HomeViewController.h extern NSString *AdStateDisabled; extern NSString *AdStateExpired; extern NSString *AdStateActive; 

Then modify your definitions to remove “static”.

 //HomeViewController.m NSString *AdStateDisabled = @"disable"; NSString *AdStateExpired = @"expired"; NSString *AdStateActive = @"active"; 

If you do not want the row user to import HomeViewController.h, you can also simply define these rows in AdState.h and put the definitions in AdState.m (and remove them from HomeViewController.m), after which the row users just need to import AdState.h to use strings.

+17
source

First delete the static file. Static variables and functions in C and Objective-C mean that they are visible only for the current compilation unit (this is more or less: only the file that you declared to the statix ​​variable can see).

Next, you also need to declare the variables in an open header file with "extern", like one of the class associated with the class:

 extern NSString *AdStateDisabled; 

Then you can use them in other files, but you would not have access to them as "Ad.AdStateDisabled", as well as "AdStateDisabled".

+8
source

Source: https://habr.com/ru/post/920836/


All Articles