Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Objective-C.Programming.pdf
Скачиваний:
11
Добавлен:
21.02.2016
Размер:
8.64 Mб
Скачать

22

Constants

We have spent a lot of time discussing variables, which as the name indicates, change their values as the program runs. There are, however, pieces of information that do not change value. For example, the mathematical constant π never changes. We call these things constants, and there are two common ways that Objective-C programmers define constants: #define and global variables.

In Xcode, create a new Foundation Command Line Tool called Constants.

In the standard C libraries, constants are defined using the #define preprocessor directive. The math part of the standard C library is declared in the file math.h. One of the constants defined there is M_PI. Use it in main.m:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])

{

@autoreleasepool {

// In literal NSString, use \u for arbitrary unicode chars NSLog(@"\u03c0 is %f", M_PI);

}

return 0;

}

When you build and run it, you should see:

π is 3.141593

To see the definition, Command-click on M_PI in the editor. It will take you to the following line in math.h:

#define M_PI

3.14159265358979323846264338327950288

Preprocessor directives

Compiling a file of C, C++, or Objective-C code is done in two passes. First, the preprocessor runs through the file. The output from the preprocessor then goes into the real compiler. Preprocessor directives start with #, and the three most popular are #include, #import, and #define.

145

Chapter 22 Constants

#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:

//Include the headers I wrote for Pet Store operations #import "PetStore.h"

//Include the headers for the OpenLDAP libraries #import <ldap.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:

#import <Foundation/Foundation.h>

The file Foundation.h has this line:

#include <CoreFoundation/CoreFoundation.h>

The file CoreFoundation.h has this line:

#include <math.h>

#define

#define tells the preprocessor, “Whenever you encounter A, replace it with B before the compiler sees it.” Look at the line from math.h again:

#define M_PI

3.14159265358979323846264338327950288

In the #define directive, you just separate the two parts (the token and its replacement) with whitespace.

#define can actually be used to make something like a function. In main.m, print the larger of two numbers:

146

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]