Correct use of IOS macros (define) and constants (const)

Source: Internet
Author: User

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 @ "Hansha haha"
Variable:
NSString *hscoder = @ "Hansha haha";
Constant:
Four ways of writing:
static const NSString *hscoder = @ "Hansha haha";
Const NSString *hscoder = @ "Hansha haha";
NSString Const *hscoder = @ "Hansha haha";
NSString * Const Hscoder = @ "Hansha haha";
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:

Write a picture description here
Constant distinction
Global constants: No matter what you define in any folder, you can access it externally
Const NSString *hscoder = @ "Hansha haha";
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 = @ "Hansha haha";
For example:
Define a Hscoder string local constant in Viewcontroller:

Write a picture description here
Compile times wrong:

Write a picture description here
What does the const modifier position mean?
1.const nsstring *hscoder = @ "Hansha haha";
2.NSString Const *hscoder = @ "Hansha haha";
3.NSString * Const Hscoder = @ "Hansha haha"; 1.const nsstring *hscoder = @ "Hansha haha";
"*hscoder" cannot be modified, "Hscoder" can be modified
2.NSString Const *hscoder = @ "Hansha haha";
"*hscoder" cannot be modified, "Hscoder" can be modified
3.NSString * Const Hscoder = @ "Hansha haha";
"Hscoder" cannot be modified, "*hscoder" can be modified
Note: 1 and 2 are really no different.
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 = @ "Hansha haha";
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
If you feel that my article is useful to you, please feel free to make a reward. Your support will encourage me to continue to create!
¥ Rewards Support

1. Will the macro allocate memory space? Is it not a direct replacement when precompiled? If you allocate memory space, that means the left value in C + +, and the macro is definitely not an lvalue.
The role of 2.static is to limit its visible scope only in this document, of course, to understand whether it is global, perhaps no problem. If you define a static const variable in the header file, you can use it only if you include the header file, but it is no longer the same variable.
Personal Insights ~ If there is a mistake welcome discussion ~
Wowbingo: @yangguang1029 I don't understand, you can look here http://blog.csdn.net/love_gaohz/article/details/7567856
This says that the macro allocates memory once each time it is replaced, as if the const is more efficient
Hansha haha: @yangguang1029 Thank you for pointing! Macros are simply replacing text in the preprocessor with no relationship to allocating memory
Hansha haha: @WOWBingo As far as I understand, the const efficiency is likely to be high, if the macro definition shaping, floating point type, there will be multiple copies, multiple copies are not said to occupy more memory, these numbers are written in the instructions
Since the use of the macro is not encouraged, why the final use of Uikit_extern?
Duke Hainting: @WOWBingo not discourage the use of macros, but instead encourage the use of macros to define constants. Because using macros to define constants can lead to unnecessary hassles, such as when computing.
Good stuff, up in the pose.
@Je_amo learn from each other??
Const nsstring* doesn't mean anything because NSString's content could not have been modified.
Const nsstring* and NSString const * indicate that the contents of the pointer cannot be changed
nsstring* const indicates that the pointer cannot be changed
I think you misunderstood.
extern NSString * Const Hscoder will not allocate memory
See here:
3.NSString * Const Hscoder = @ "Hansha haha";
"Hscoder" cannot be modified, "*hscoder" can be modified
Test a bit, *hscoder can not be modified ah, how to modify?
If you find a numeric type, it is no problem to write it.
You create this standalone file HSConst.h and HSCONST.M are inherited from what

Correct use of IOS macros (define) and constants (const)

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.