简体   繁体   中英

define - constant or literal Objective-C

We have the following code (in the .h. or .m file of Objective-C app)

#define SQUARE_SIZE 28
#define APP_DELEGATE [[UIApplication sharedApplication] appDelegate]

are both of them constants or literals ?

What is their proper name? And it they are literals as I suspect, why most call them constants :)?

They're neither, they're macros.

A constant would look like this:

const int square_size = 28;

The difference between a macro and a constant here is that the constant has a type while a macro is simply some text fragment that gets inserted at the place where it's mentioned before the compiler is parsing the source code.

And a literal is something like 28 or @"This is a string" , that is a value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM