"Go" IOS macro (define) and the correct use of constants (const)-Good

Source: Internet
Author: User

Original URL: http://www.jianshu.com/p/f83335e036b5

In iOS development, macro definitions are often used, or some data types are modified with const, and often developers do not know how to use them correctly, resulting in the confusion of macros and const decorations in the project.

Can you distinguish between the following? Do you know when to use it?

#define Hscoder @"Hansha haha"nsstring *hscoder = @ "Hansha haha";  extern NSString *hscoder;  extern const nsstring *hscoder;  Static Const NSString *hscoder = @ "Hansha haha";  Const NSString *hscoder = @ "Hansha haha";  NSString Const *hscoder = @ "Hansha haha";  NSString * Const Hscoder = @ "Hansha haha";            
When we want to share some data globally, we can use macros, variables, constants

Macro:

#define HSCoder @"汉斯哈哈哈"

Variable:

NSString *HSCoder = @"汉斯哈哈哈";

Constant:

四种写法:static const NSString *HSCoder = @"汉斯哈哈哈";const NSString *HSCoder = @"汉斯哈哈哈";NSString const *HSCoder = @"汉斯哈哈哈";NSString * const HSCoder = @"汉斯哈哈哈";

Thinking: Macro and constant/variable selection?

    • Macro: Just text substitution in the preprocessor, no type, no type checking, the compiler can optimize the same string. Save only one copy to the. Rodata segment. Even strings with the same suffix can be optimized, you can compile tests with gcc, "Hello World" and "world" two strings, storing only the previous one. Take only the front and middle address, if it is shaped, floating-point type will have multiple copies, but these numbers are written in the instructions. Only the code snippet, a large number of macros will cause the binary file to become larger
    • Variables: Share a piece of memory space, even if the project is used in N, also will not allocate n block memory space, can be modified, in the compilation phase will perform type checking
    • Constant: Share a piece of memory space, even if the project is used in N, do not allocate n block of memory space, can be modified according to the location of the const modification, in the compilation phase will perform type checking

Use const as much as possible to see the Apple API using constant multipoint, such as:


Here to write a picture describing constant distinction

Global constants: No matter what you define in any folder, you can access it externally

const NSString *HSCoder = @"汉斯哈哈哈";

For example:

Define a Hscoder string global constant in Viewcontroller:


Write a picture description here

Access in Appdelegate:


Write a picture description here

Local constants: Cannot provide outside access after being modified with static

static const NSString *HSCoder = @"汉斯哈哈哈";

For example:

Define a Hscoder string local constant in Viewcontroller:


Write a picture description here

Compile times wrong:


Here are the pictures that describe the const modifier locations, which represent what?
1. Const nsstring *hscoder = @ "Hansha haha"; nsstring const *hscoder = @ " Hansha haha "; 3.nsstring * const HSCoder = @ "Hansha haha";              
1.const NSString *HSCoder = @"汉斯哈哈哈";"*HSCoder"不能被修改, "HSCoder"能被修改2.NSString const *HSCoder = @"汉斯哈哈哈";"*HSCoder"不能被修改, "HSCoder"能被修改3.NSString * const HSCoder = @"汉斯哈哈哈";"HSCoder"不能被修改,"*HSCoder"能被修改注意:1和2其实没什么区别

Conclusion: The right side of Const cannot be modified

Verify:

const int *p;

Write a picture description here
int const *p;

Write a picture description here
int * const p;

Write a picture description here

So generally we define a constant and don't want to be modified like this:

NSString * const HSCoder = @"汉斯哈哈哈";

Test:

Define a constant Hscoder in Viewcontroller:


Write a picture description here

In Appdelegate Modify constant Hscoder, see the following error prompt:


Write a picture description here

In a generic project, define global constants, which are written in separate files

HSCONST.M Defining constants:


Write a picture description here

HSConst.h provides external access constants:


Write a picture description here

Access in Appdelegate:


Write a picture description here



Wen/Hansha haha (Jane book author)
Original link: http://www.jianshu.com/p/f83335e036b5
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

"Go" IOS macro (define) and the correct use of constants (const)-Good

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.