Use the extern keyword method:
1, the new Constants.h file (file name according to the need to take), for storing global variables;
2. Write the global variable name you need in Constants.h, but you cannot initialize it when you define it, or you will get an error such as:
NSString *url;//pointer type
int count;//non-pointer type
3. Introduce this file in a file that requires a global variable:
#import "Constants.h"
4. Initialize the global variable or assign a value:
- extern nsstring *url;
- URL = [[NSString alloc] initwithformat:@"http://www.hbcfg.net"];//pointer type; required Alloc
- extern int count;
- Count = 0; //non-pointer type
The use of global variables is the same as using normal variables.
Of course there are other ways: You can declare and initialize global variables in appdelegate, use singleton access to global variables, and so on.
iOS development uses extern to access global variables