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

NSMutableArray

#import <Foundation/Foundation.h>

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

{

@autoreleasepool {

//Create three NSDate objects NSDate *now = [NSDate date];

NSDate *tomorrow = [now dateByAddingTimeInterval:24.0 * 60.0 * 60.0]; NSDate *yesterday = [now dateByAddingTimeInterval:-24.0 * 60.0 * 60.0];

//Create an array containing all three (nil terminates the list)

NSArray *dateList = [NSArray arrayWithObjects:now, tomorrow, yesterday, nil];

for (NSDate *d in dateList) { NSLog(@"Here is a date: %@", d);

}

}

return 0;

}

This type of loop is known as fast enumeration, and it is an extremely efficient way to walk through the items in an array. There is one restriction: you may not add to or remove from the array while enumerating over it.

NSMutableArray

Arrays come in two flavors:

An instance of NSArray is created with a list of pointers. You can never add or remove a pointer from that array.

An instance of NSMutableArray is similar to an instance of NSArray, but you can add and remove pointers. (NSMutableArray is a subclass of NSArray. More about subclasses in Chapter 18.)

Change your example to use an NSMutableArray instance and methods from the NSMutableArray class:

#import <Foundation/Foundation.h>

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

{

@autoreleasepool {

//Create three NSDate objects NSDate *now = [NSDate date];

NSDate *tomorrow = [now dateByAddingTimeInterval:24.0 * 60.0 * 60.0]; NSDate *yesterday = [now dateByAddingTimeInterval:-24.0 * 60.0 * 60.0];

//Create an empty array

NSMutableArray *dateList = [NSMutableArray array];

// Add the dates to the array [dateList addObject:now]; [dateList addObject:tomorrow];

89

'""@-+ 5

! " # "

R#-: ; U" # " # IU&S

2A " D % &

2AD ['( Q['! %,

.

W " # "

R#-: ; L # IU&S

N6-T N # 0T 1 R#-; L # IU&S

.

-,

.

&& '

+ ''&! % ' N6%! L "@ ! ( " * ' ! 3K )G " !4 , ) & )' ) ) !

( ; + ' ! ) & + ' + & '' ' + @ G !

@ @

! " #$%

&

[ &

X ? %

2AA " A

I #2AA ) > N @ [' '

2A3;@8A V

23DD$,

B G ?

2A ? " I # A A B?A ['+ '$,

1 ?

2AA " % &

D G ' '

2AX I # N A [' ' 2A> / A$,

) K

*I 2A2 @ % &

2AD ['Q['! %,

.

.

.

-,

.

( & '' ' ! ( & 0 ) + ' ! B ' " &

34

%%

'' ' ) + ! , ; ' Q0 R ) G ' Q R+ " ! + & ' F !

B ' ) ) ' & +! ( ' ) ' 2 U!

2AA " I [' B>',

2AA " I [' ',

# / >$II 2AN A % &

2AD [' Z '%,

.

# / >$II 2AN % &

2AD [' '%,

.

# / >$II 2AN % &

2AD [' '%,

.

3

This page intentionally left blank

16

Developer Documentation

In the last chapter, I gave you some useful tidbits about the class NSArray. When you begin programming without my guidance (including taking on Challenges in this book), you will need to find such tidbits yourself. This is where Apple’s documentation comes in.

To see the documentation in Xcode, bring up the Organizer (click the button at the top right of the Xcode window) and choose the Documentation tab.

Figure 16.1 Docs in the Organizer

There are essentially five types of documentation that Apple provides:

References: Every Objective-C class and every C function is tersely documented in these pages.

Guides and Getting Starteds: Guides are less terse and group ideas conceptually. For example, there is an Error Handling Programming Guide that describes the myriad of ways that Mac and iOS developers can be informed that something has gone wrong.

93

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