Experience-use const and macro in PCH

Source: Internet
Author: User

Experience-use const and macro in PCH
Most of the time, due to the development needs, we often need to define some variables used in the entire project in pch. In general, in the previous development process, we are all used to writing a macro directly in pch. However, after carefully analyzing the official documents of apple, we will find that some variables defined in it are not defined by macros, but by using const. Then we need to know why we use const, first, let's take a look at some basic usage of const. In the C language syntax, if we break some variables in const, let's take a look at what will happen. This is also a frequently asked question during the interview:
Let's take a brief look:

Modifier constant:

<code class=" hljs cs">void testConst(){    const int age1 = 20;    int const age2 = 30;}</code>

In this case: the effect is the same. In this case, age1 \ age2 is a constant and is read-only.

When the pointer is modified
The proximity principle is equivalent to a constant which cannot be assigned a value or pointed to again.

<Code class = "hljs cs"> void testConst2 () {int age = 20; // The modifier * p1 and * p2 of const, * p1 and * p2 are constants, the const int * p1 = & age; int const * p2 = & age; int const * p2 = & age; int num = 30; p1 = # p2 = # // p3 modified by const, p3 is a constant, and p3 cannot point to other variables int * const p3 = & age; // incorrect Writing Method // int num = 30; // p3 = # // correct writing method // * p3 = 30;} </code>

* P1 and * p2 modified by const, * p1 and * p2 are constants, and the values of other variables cannot be indirectly modified through p1 and p2 pointers.

So how should we use it in the project?
We define a class: for example, this class contains some data in our project.
In the ZYConst. h file:

<Code class = "hljs objectivec"> # import <foundation. h = ""> // notification // extern NSString * const ZYEmotionDidSelectNotification; extern NSString * const ZYSelectEmotionKey; // delete text notification extern NSString * const zyemotiondiddeletenoication ication; </foundation> </code>

In ZYConst. m:

<Code class = "hljs objectivec"> # import <foundation. h = ""> // notification // NSString * const ZYEmotionDidSelectNotification = @ "ZYEmotionDidSelectNotification"; NSString * const ZYSelectEmotionKey = @ "ZYSelectEmotionKey "; // notification of text deletion NSString * const ZYEmotionDidDeleteNotification = @ "ZYEmotionDidDeleteNotification"; </foundation> </code>

In pch, we can directly import this ZYConst file.

#import "ZYConst.h"

Then some variables defined in this class will be available in the entire project.

Benefits:

If you use the const modifier, there is only one copy in the memory, so no matter where you use it in the project, it is the same. Therefore, we strongly recommend that you use macros: macros are the macro content we define during compilation, which is directly compiled into the string we write, so there may be multiple creation and multiple calls.
Note:
In some cases, const cannot replace macros, such:
// RGB color # define ZYColor (r, g, B) [UIColor colorWithRed :( r)/255.0 green :( g)/255.0 blue :( B)/255.0 alpha: 1.0] // random color # define ZYRandomColor HWColor (arc4random_uniform (256), arc4random_uniform (256), arc4random_uniform (256 ))

At this time, you cannot use const, because the content after const cannot be calculated, but it is something dead.

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.