To see this type M_PI in an Xcode project. Then hold down the Command key (⌘) and click on it.
Before your program runs the string M_PI is replaced with the string 3.14159265358979323846264338327950288.
It’s a lot easier to just type M_PI!
When you compile a C or Objective-C program, something called the preprocessor runs first. It looks for preprocessor directives. Here are the important ones.
The #include directive will copy in the file that comes after it. It actually finds the file and inserts into your code. It will do this everywhere you use an #include. That can lead to multiple copies of the file being in your project. That’s not such a good thing.
It’s used like this.
It is used by C programmers because that’s all they have.
Objective-C brought the #import directive to the compiler. It works the same as #include, but it will not bring more than one copy of a file into your project. That’s better!
Objective-C programmers should use #import.
The #define directive is a search-and-replace command. Whatever comes right after the #define will be replaced with what comes after that.
We can write our own defines at the beginning of our Objective-C files.
Global extern Constants
The best way to create a global constant that has a single value is with a extern constant.
We need a class to create these. Here’s an example of a class named Constants. In the .h file we have this.
Here’s the .m file.
Then we import the class and use the constant.
The extern keyword means that the constant will be defined in another file and that only one copy will exist in this project.
The const keyword means that it is a constant and can’t be changed in the application.
An enum lets us define multiple values that are related. Here’s an example.
ClockDisplayType12Hour will have a value of zero.
ClockDisplayType24Hour will have a value of one.
#define vs global variables
For almost all circumstances when coding Objective-C you should use global extern variables.