Online Book Reader

Home Category

Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [50]

By Root 429 0
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;

#define vs global variables


Given that you can define a constant using #define or a global variable (which includes the use of enum), why do Objective-C programmers tend to use global variables? In some cases, there are performance advantages to using global variables. For example, you can use == instead of isEqual: to compare strings if you consistently use the global variable (and an arithmetic operation is faster than a message send). Also, global variables are easier to work with when you are in the debugger.

You should use global variables and enum for constants, not #define.

23

Writing Files with NSString and NSData


The Foundation framework gives the developer a few easy ways to read from and write to files. In this chapter, you’ll try a few of them out.

Writing an NSString to a file


First, let’s see how you would take the contents of an NSString and put it into a file. When you write a string to a file, you need to specify which string encoding you are using. A string encoding describes how each character is stored as an array of bytes. ASCII is a string encoding that defines the letter ‘A’ as being stored as 01000001. In UTF-16, the letter ‘A’ is stored as 0000000001000001.

The Foundation framework supports about 20 different string encodings. UTF can handle an incredible collection of writing systems. It comes in two flavors: UTF-16, which uses two or more bytes for every character, and UTF-8, which uses one byte for the first 128 ASCII characters and two or more for other characters. For most purposes, UTF-8 is a good fit.

Create a new project: a Foundation Command Line Tool called Stringz. In main(), use methods from the NSString class to create a string and write it to the filesystem:

#import

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

@autoreleasepool {

NSMutableString *str = [[NSMutableString alloc] init];

for (int i = 0; i < 10; i++) {

[str appendString:@"Aaron is cool!\n"];

}

[str writeToFile:@"/tmp/cool.txt"

atomically:YES

encoding:NSUTF8StringEncoding

error:NULL];

NSLog(@"done writing /tmp/cool.txt");

}

return 0;

}

This program will create a text file that you can read and edit in any text editor. The string /tmp/cool.txt is known as the file path. File paths can be absolute or relative: absolute paths start with a / that represents the top of the file system, whereas relative paths start at the working directory of the program. Relative paths do not start with a /. In Objective-C programming, you’ll find that we nearly always use absolute paths because we typically don’t know what the

Return Main Page Previous Page Next Page

®Online Book Reader