Global variables in Objective-C

In ActionScript, you can have global variables like this:

var number : Number = 15; 

And then use it in the method / function. How do you do this in Objective-c, is this possible?

+1
source share
1 answer

Remember that Objective-C is a strict superset of C, so you can declare global variables just like regular C. First, declare them in a file outside of any function, then use the C extern keyword in other files to pull these variables.

If you want to do this with more than C variables, but rather use Objective-C objects, you can save them in the applicationโ€™s deletion. Just set them there, as usual, when you need to access the variable:

 // Assuming your app delegate is of class YourAppDelegate and // has an NSString* variable called globalString: YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate]; NSString *someGlobalString = [appDelegate globalString]; 

It may also be useful for you to declare a static variable in the application delegate.

+6
source

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


All Articles