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

Global variables

#import <Foundation/Foundation.h>

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

{

@autoreleasepool {

NSLog(@"\u03c0 is %f", M_PI);

NSLog(@"%d is larger", MAX(10, 12));

}

return 0;

}

MAX is not a function; it is a #define. The most basic C version of MAX is:

#define MAX(A,B)

((A) > (B) ? (A) : (B))

So, by the time the compiler saw the line you just added, it looked like this:

NSLog(@"%d is larger", ((10) > (12) ? (10) : (12)));

When you use #define to do function-like stuff instead of simply substituting a value,you are creating a macro.

Global variables

Instead of using #define, Objective-C programmers commonly use global variables to hold constant values.

Let’s add to your program to explain. First, there is a class named NSLocale that stores information about different geographical locations. You can get an instance of the user’s current locale and then ask it questions. For instance, if you wanted to know what the currency is in the user’s locale, you could ask for it like this:

#import <Foundation/Foundation.h>

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

{

@autoreleasepool {

NSLog(@"\u03c0 is %f", M_PI);

NSLog(@"%d is larger", MAX(10, 12));

NSLocale *here = [NSLocale currentLocale];

NSString *currency = [here objectForKey:@"currency"];

NSLog(@"Money is %@", currency);

}

return 0;

}

Build and run it. Depending on where you are, you should see something like

Money is USD

If, however, you mistype the key as @"Kuruncy", you won’t get anything back. To prevent this problem, the Foundation framework defines a global variable called NSLocaleCurrencyCode. It isn’t easier to type, but if you do mistype it, the compiler will complain. Also, code completion in Xcode works

147

Chapter 22 Constants

properly for a global variable, but not for the string @"currency". Change your code to use the constant:

#import <Foundation/Foundation.h>

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

{

@autoreleasepool {

NSLog(@"\u03c0 is %f", M_PI);

NSLog(@"%d is larger", MAX(10, 12));

NSLocale *here = [NSLocale currentLocale];

NSString *currency = [here objectForKey:NSLocaleCurrencyCode];

NSLog(@"Money is %@", currency);

}

return 0;

}

When the class NSLocale was written, this global variable appeared in two places. In NSLocale.h, the variable was declared something like this:

extern NSString const *NSLocaleCurrencyCode;

The const means that this pointer will not change for the entire life of the program. The extern means “I promise this exists, but it will be defined in some other file.” And sure enough, in the file NSLocale.m, there is a line like this:

NSString const *NSLocaleCurrencyCode = @"currency";

enum

Often you will need to define a set of constants. For example, imagine that you were developing a blender with five speeds: Stir, Chop, Liquefy, Pulse, and Ice Crush. Your class Blender would have a method called setSpeed:. It would be best if the type indicated that only one of the five speeds was allowed. To do this, you would define an enumeration:

enum BlenderSpeed { BlenderSpeedStir = 1, BlenderSpeedChop = 2, BlenderSpeedLiquify = 5, BlenderSpeedPulse = 9, BlenderSpeedIceCrush = 15

};

@interface Blender : NSObject

{

// speed must be one of the five speeds enum BlenderSpeed speed;

}

// setSpeed: expects one of the five speeds - (void)setSpeed:(enum BlenderSpeed)x;

@end

Developers get tired of typing enum BlenderSpeed, so they often use typedef to create a shorthand for it:

148

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