Effective OBJECTIVE-C 2.0-fourth: Multi-use type constants, less # define preprocessing directives

Source: Internet
Author: User

Fourth: Multi-use type constants, less # define preprocessing directives

Disadvantages of using # define preprocessing: The defined constants do not have type information, and the compiler simply performs a find-and-replace operation on this basis before compiling. Even if a constant value is redefined, the compiler does not produce a warning message, which results in inconsistent constant values in the application.

Use for example: static const nstimeinterval kanimationduration = 0.3;

Use the static const in the implementation file to define constants that are visible only within the compilation unit. Because such constants are no longer in the global symbol table, you do not need to prefix their names.

If the constant is confined to an "implementation file", precede with the letter K, such as kanimationduration; If the constant is visible outside the class, it is usually prefixed with the class name.

Meaning: If the view modifies the variable declared by the const, then the compiler will make an error.

If the variable is declared without static, the compiler creates an "external symbol" (external symbol), and the compiler throws an error message if a variable with the same name is also declared in another compilation unit: Duplicate symbol-- Kanimationduration in:

eocanimatedview.o

eocotherview.o

Both declared as static and const do not create symbols, scope @define, and defined constants with type information.

Compilation unit: Implementation file for each class (with. m as the suffix name)

You need to expose a constant, which needs to be placed in the global symbol table, so that it can be used outside of the variant unit that defines the constant. Use extern in the header file to declare global constants and define their values in the relevant implementation file. Prefix the class name associated with the application.

In the header file

  extern        NSString *const eocstringconstant; Declared in the header file.

Eocstringconstant is "a constant, and this constant is a pointer to the NSString object"

In the implementation file

NSString *const eocstringconstant = @ "VALUE"//defined in implementation file

The extern keyword, meaning to tell the compiler that there will be a symbol named Eocstringconstant in the global symbol table.

That is, the compiler looks at its definition in an unordered order, which allows the code to use this constant. Because it knows that this constant will definitely be found when it is linked to a binary file.

Such constants must be defined and defined only once. When the target file is generated by the implementation file, the compiler allocates space for the string in the data section. The linker will link this target file to other destination files to generate the final binary file. Wherever the eocstringconstant is used, the connector can parse the global symbol.

Effective OBJECTIVE-C 2.0-fourth: Multi-use type constants, less # define preprocessing directives

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.