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 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";
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:
typedef enum { BlenderSpeedStir = 1, BlenderSpeedChop = 2, BlenderSpeedLiquify = 5, BlenderSpeedPulse = 9, BlenderSpeedIceCrush = 15 } BlenderSpeed; @interface Blender : NSObject { // speed must be one of the five speeds BlenderSpeed speed; } // setSpeed: expects one of the five speeds - (void)setSpeed:(BlenderSpeed)x; @end
Often you won’t care what numbers the five speeds represent – only that they are different from each other. You can leave out the values, and the compiler will make up values for you:
typedef enum { BlenderSpeedStir, BlenderSpeedChop, BlenderSpeedLiquify, BlenderSpeedPulse, BlenderSpeedIceCrush } BlenderSpeed;