IOS extern usage tutorial

Source: Internet
Author: User
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:

  1. Extern nsstring * URL;
  2. Url = [[nsstring alloc] initwithformat: @ "http://www.hbcfg.net"]; // pointer type; alloc required
  3. Extern int count;
  4. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.