Usage in Experience-PCH: const and macro

Source: Internet
Author: User

    • Many times, due to the need for development, we often need to define in the PCH some variables that are used throughout the project, etc.
    • In general, during the previous development process, we were accustomed to writing a macro directly in the PCH to get it done. But when we carefully analyze Apple's official documents, we will find that some of the variables defined in him are not defined by macros, but with the const
    • Then we need to know why the use of const, first understand some of the basic use of const, in the C language syntax, if we have a const break some variables what happens, a simple look, this is the interview often asked:
      So let's review it briefly:

Modifier constants:

void testConst(){    constint20;    intconst30;}

When this is the case: the effect is the same, this time age1\age2 is constant, read-only

when the pointer is decorated, the sub-condition
Nearest principle close to who is equivalent to a constant cannot be re-assigned or re-directed

voidTestConst2 () {intAge = -;//Const modified *P1 and *P2,*P1 and *P2 are constants that cannot be indirectly modified by P1, P2 pointers to the values of other variables    Const int*P1 = &age;int Const*P2 = &age;intnum = -;    P1 = # P2 = #//Const-Modified P3,P3 is a constant, P3 can no longer point to other variables    int*ConstP3 = &age;//Incorrect notation    //int num =;    //p3 = #    //correct wording    //*p3 = +;}

Const-Decorated *P1 and *P2,*P1 and *P2 are constants that cannot be indirectly modified by P1, P2 pointers to the values of other variables

So what should we do with the project?
We define a class: for example, some of the data in our project is in this class.
In the ZYConst.h file:

#import <Foundation/Foundation.h>// 通知// 表情选中的通知externNSStringconst ZYEmotionDidSelectNotification;externNSStringconst ZYSelectEmotionKey;// 删除文字的通知externNSStringconst ZYEmotionDidDeleteNotification;

In ZYCONST.M:

#import <Foundation/Foundation.h>// 通知// 表情选中的通知NSStringconst ZYEmotionDidSelectNotification = @"ZYEmotionDidSelectNotification";NSStringconst ZYSelectEmotionKey = @"ZYSelectEmotionKey";// 删除文字的通知NSStringconst ZYEmotionDidDeleteNotification = @"ZYEmotionDidDeleteNotification";

Well, we'll just import this zyconst file in the PCH.

#import "ZYConst.h"

So in the whole project, there are some variables that we define in this class.

Benefits:

    • With the const modifier, there is only one copy in memory, so no matter where you use it in your project, this is the one, so it is highly recommended
    • The use of macro words: Macros are compiled at the time of our definition of the macro content, directly compiled into the string we write, then there may be multiple creation, multiple calls.
      Note
      In some cases, a const is not a substitute for a macro, such as:
// RGB颜色#defineZYColor(r, g, b)[UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]// 随机色#defineHWColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))

At this point, you cannot use const, because the content behind the const cannot be a result of some computation, but something that is dead.

Usage in Experience-PCH: const and macro

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.