IOS development uses extern to access global variables
Use the extern keyword method:
1. Create a new constants. h file (the file name can be retrieved as needed) to store global variables;
2. Write the required global variable name in constants. h, but do not initialize it during definition. Otherwise, an error occurs, for example:
Nsstring * URL; // pointer type
Int count; // non-pointer type
3, Introduce this file in the file that requires the use of global variables:
# Import "Constants. H"
4. initialize or assign values to global variables:
- Extern nsstring * URL;
- Url = [[nsstring alloc] initwithformat: @ "http://www.hbcfg.net"]; // pointer type; alloc required
- Extern int count;
- Count = 0; // non-pointer type
The use of global variables is the same as that of common variables.
Of course, there are other methods: You can declare and initialize global variables in appdelegate, and use a single instance to access global variables.