Usage of extern,static,const in OC

Source: Internet
Author: User

The role of 1.const:

Const is used only to modify the right variable (basic data variable p, pointer variable *p). For example NSString *const sialertviewwilldismissnotification;

Const-Modified variables are read-only

The role of 2.static:
    • To modify a local variable:

1. Extend the life cycle of a local variable, and the program ends before it is destroyed.

2. Local variables generate only one copy of memory and are initialized only once.

3. Change the scope of the local variable.

    • modifying global variables

1. Can only be accessed in this file, modify the scope of global variables, the life cycle will not change

2. Avoid repeating the definition of global variables

3..PCH generated global variables are one copy of each file, so the modification of the file's global variables can not be modified across files

The role of extern
    • A value that is used to get global variables (including global static variables) and cannot be used to define variables
    • extern works: First in the current file to find there are no global variables, not found, will go to other files to find.

(for example, another. h or. m file declares a global variable that you want to use on the current page)

The other interface Appdelegate declares a

NSString *const kdevicetokenstring = @ "kdevicetokenstring";

Current interface

extern nsstring *const kdevicetokenstring;



Learn the link and thank the author for sharing
Original link: http://www.jianshu.com/p/9fd82a6d016b

Usage of extern,static,const in OC

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.