Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of the #define in the objective C?

I want to ask a question about the objective-C or may be the C language. I want to ask what is the meaning of the following code of #define? Is it like to declare a variable?

#define kMountainNameString         @"name"
#define kMountainHeightString           @"height"
#define kMountainClimbedDateString      @"climbedDate"
like image 421
Questions Avatar asked Jul 30 '10 04:07

Questions


People also ask

What type of word is the?

The word the is considered a definite article because it defines the meaning of a noun as one particular thing. It's an article that gives a noun a definite meaning: a definite article. Generally, definite articles are used to identify nouns that the audience already knows about.

What is in English the?

It is the definite article in English.

What is the word the in grammar?

The definite article (the) is used before a noun to indicate that the identity of the noun is known to the reader. The indefinite article (a, an) is used before a noun that is general or when its identity is not known. There are certain situations in which a noun takes no article.

What is the meaning of UwU?

or UwU (ˈuːwuː ) exclamation. slang. an expression of admiration for something that is considered cute, used esp in text messaging and social media.


3 Answers

It's a simple text substitution macro. Works the same as in C, C++.

Where kMountainNameString appears the compiler will "paste in" @"name". Technically speaking this occurs before the compiler by a mechanism called the preprocessor.

like image 147
seand Avatar answered Sep 30 '22 19:09

seand


#define is a preprocessor directive inherited from C that takes the form

#define identifier [value]

In general, it is used to tell the preprocessor to replace all instances of identifier in the code with the given text before passing it on to the compiler. Identifiers can also be defined without values to be used as compiler flags to prevent multiple definitions of the same variables, or to branch on machine details that will not change during execution. For example, to pass different code to the compiler based on the architecture of your processor you could do something like:

#ifdef INTEL86
//some 32-bit code
#else
//some 64-bit code
#endif

When assigning values in these definitions, it's often a good idea to surround the value with parentheses so as to preserve it as one unit, regardless of the context it exists in.

For example, #define FOO 3 + 7 has a different effect than #define FOO (3 + 7) on the result of the following line, due to the order of arithmetic operations:

a = 3 * FOO

See this link for more details on preprocessor directives in general or this link for information more focused on Objective C.

like image 32
psicopoo Avatar answered Sep 30 '22 19:09

psicopoo


The preprocessor replaces all occurences of kMountainNameString with @"name" before compilation begins.

like image 36
Plumenator Avatar answered Sep 30 '22 17:09

Plumenator