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

Challenge

If you find yourself creating property lists by hand, you should know that Xcode has a built-in editor specifically for property lists.

Now add the code that reads the file in:

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

{

@autoreleasepool {

NSMutableArray *stocks = [[NSMutableArray alloc] init];

NSMutableDictionary *stock;

stock = [NSMutableDictionary dictionary]; [stock setObject:@"AAPL"

forKey:@"symbol"];

[stock setObject:[NSNumber numberWithInt:200] forKey:@"shares"];

[stocks addObject:stock];

stock = [NSMutableDictionary dictionary]; [stock setObject:@"GOOG"

forKey:@"symbol"];

[stock setObject:[NSNumber numberWithInt:160] forKey:@"shares"];

[stocks addObject:stock];

[stocks writeToFile:@"/tmp/stocks.plist" atomically:YES];

NSArray *stockList = [NSArray arrayWithContentsOfFile:@"/tmp/stocks.plist"];

for (NSDictionary *d in stockList) { NSLog(@"I have %@ shares of %@",

[d objectForKey:@"shares"], [d objectForKey:@"symbol"]);

}

}

return 0;

}

Build and run the program.

Challenge

Write a tool that creates a property list that has all 8 types in it: array, dictionary, string, data, date, integer, float, boolean.

173

This page intentionally left blank

Part IV

Event-Driven Applications

Here’s where we’ve been heading and why you’ve been reading this book – writing iOS and Cocoa apps. In the next two chapters, you’ll get a taste of application development. Your applications will have a GUI (graphical user interface), and they will be event-driven.

In a command-line program, you execute the program, and then it does its own thing until it’s all finished. An event-driven application is different. It launches and then starts a run loop which waits for events. When an event happens, the application leaps into action, executing methods, sending messages, etc.

First, you’ll write an iOS application and then a similar Cocoa application. Cocoa is the collection of frameworks written by Apple that you use to write applications on the Mac. You’re already familiar with one of these frameworks – Foundation.

To write iOS apps, you use another set of frameworks called Cocoa Touch. Cocoa and Cocoa Touch have some frameworks in common, like Foundation. Others are specific to one platform or the other.

This page intentionally left blank

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