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

Source: Internet
Author: User

In iOS development, it is often used to define macros, or to modify some data types 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 tell the difference 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*ConstHscoder = @"Hansha haha";
When we want to share some data globally, we can use macros, variables, constants

Macro:

#define HSCoder @"汉斯哈哈哈"

Variable:

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

Constant:

四种写法:staticconstNSString *HSCoder = @"汉斯哈哈哈";constNSString *HSCoder = @"汉斯哈哈哈";NSStringconst *HSCoder = @"汉斯哈哈哈";NSStringconst HSCoder = @"汉斯哈哈哈";

Thinking: Macro and constant/variable selection?

    • Macro: Simply replace the text in the preprocessor. No type, no matter what type of check, the compiler is able to optimize the same string. Just save one copy to the. Rodata section.

      Even strings with the same suffix can be optimized, and you can compile the test with GCC, "Hello World" and "world" two strings. Just store the previous one. Take only the front and middle address, assuming that the shape, floating-point type will have multiple copies, but these numbers are written in the instructions. is 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 n is used in the project, will not allocate n block memory space. Can be changed
    • Constants: Share a piece of memory space, even if n is used in the project. Also does not allocate n block memory space, can be changed according to the location of the const modification

Use const as much as possible, see the Apple API using constant multipoint, for example by:

Constant distinction

Global constants: No matter what directory you define, you can access it from outside

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

Like what:

Define a Hscoder string global constant in Viewcontroller:

Interview in Appdelegate:

Local constants: After being decorated with static. Cannot provide outside access

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

Like what:

Define a Hscoder string local constant in Viewcontroller:

Compile times wrong:

The const modifier location is different. What does it 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 changed.  "Hscoder"  can be changed 2.  nsstring  const  *hscoder = @ "Hansha haha" ;  "*hscoder"  cannot be changed,  "Hscoder"  can be changed 3.  nsstring  * const  hscoder = @" Hansha haha ";  "Hscoder"  cannot be changed,  "*hscoder"  can be changed note: 1  and 2  in fact no difference  

Conclusion: The right side of Const cannot be changed

Verify:

constint *p;

intconst *p;

intconst p;

So in general, we define a constant and don't want to be altered like this:

NSStringconst HSCoder = @"汉斯哈哈哈";

Test:

Define a constant Hscoder in Viewcontroller:

Change the constant hscoder in appdelegate. See the following error message:

In a generic project, define global constants. will be written in a separate file.

HSCONST.M Defining constants:

HSConst.h provides external access constants:

Interview in Appdelegate:

My book of Jane: Http://www.jianshu.com/users/368a8cd349af/latest_articles

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.