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

#define vs global variables

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.

149

This page intentionally left blank

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 <Foundation/Foundation.h>

 

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

151

) ""0 ( %$@--@-6

+ & & ' + " + * & '! " + ! > @ " '' )G & ++ ) ) ) + G * + + + * &' ! 3( & ) ' ) '!4

) ' & + + ) + & ! , ; ' ) ' " + + & + ) ! > ' ; ! > & ' ' &) ! , ) * + ' ' ' ' + ) & + + + " ) & ) & ) !

& ' E6 + ) &) ) ' ) " ) ) ) & ! K ) &) 3 ' 4 & " +' ) " ) ! ( & ' ' & " !

, ' ' *N65 & ! ; ' " 7

@ @

! " #$% &

[ &

2AP A " I ##2AP A$ $,

I-, 9-, JJ% &

# A [' *+ '$,

.

' N65 ; 1 ! # (

. N65 " # 1 1

N65 )

N65 " N66 #

9::-! ) R . ? UT I

"UY56

# UN6H.?B6 5 #

UM S

. ! # 9::-1 # O! " N65 #

! 7

N6-T # I

8 7

N6-T I #U 0T 1 R D #' S

8

.

-,

.

) ) ! + + ' & G ; *['E '! K ) ) & ' !

) &N65) ) GN65@ !

')

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