#include and #import

#include and #import do essentially the same thing: request that the preprocessor read a file and add it to its output. Usually, you are including a file of declarations (a .h file), and those declarations are used by the compiler to understand the code it is compiling.

What is the difference between #include and #import? #import ensures that the preprocessor only includes a file once. #include will allow you to include the same file many times. C programmers tend to use #include. Objective-C programmers tend to use #import.

When specifying the name of the file to be imported, you can wrap the filename in quotes or angle brackets. Quotes indicate that the header is in your project directory. Angle brackets indicate that the header is in one of the standard locations that the preprocessor knows about. (<math.h>, for example, is /Developer/SDKs/MacOSX10.7.sdk/​usr/include/math.h.) Here are two examples of #import directives:

/​/​ ​I​n​c​l​u​d​e​ ​t​h​e​ ​h​e​a​d​e​r​s​ ​I​ ​w​r​o​t​e​ ​f​o​r​ ​P​e​t​ ​S​t​o​r​e​ ​o​p​e​r​a​t​i​o​n​s​
#​i​m​p​o​r​t​ ​"​P​e​t​S​t​o​r​e​.​h​"​

/​/​ ​I​n​c​l​u​d​e​ ​t​h​e​ ​h​e​a​d​e​r​s​ ​f​o​r​ ​t​h​e​ ​O​p​e​n​L​D​A​P​ ​l​i​b​r​a​r​i​e​s​
#​i​m​p​o​r​t​ ​<​l​d​a​p​.​h​>​

In a project, it used to be pretty common to include a collection of headers in every file of code. This led to clutter at the beginning of your file and made compiles take longer. To make life easier and compiles faster, most Xcode projects have a file that lists headers to be precompiled and included in every file. In your Constants project, this file is called Constants-Prefix.pch.

So, how did a constant from math.h get included when main.m was compiled? Your main.m file has the following line:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

The file Foundation.h has this line:

#​i​n​c​l​u​d​e​ ​<​C​o​r​e​F​o​u​n​d​a​t​i​o​n​/​C​o​r​e​F​o​u​n​d​a​t​i​o​n​.​h​>​

The file CoreFoundation.h has this line:

#​i​n​c​l​u​d​e​ ​<​m​a​t​h​.​h​>​
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset