Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [58]
"-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
Nice, eh? Human-readable. XML. One line of code.
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.
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.
27
Your First iOS Application
In this chapter, you are going to create an iOS application: a simple to-do list application called iTahDoodle that stores its data as a property list. Here’s what iTahDoodle will look like when you’re done.
Figure 27.1 Complete iTahDoodle application
All iOS applications are event-driven applications. The run loop waits for events. The waiting application then responds to events generated by the user (like a button tap) or the system (like a low-memory warning).
Getting started with iTahDoodle
In Xcode, choose File → New → New Project.... Under the iOS section (not the Mac OS X section), click on Application. From the template choices that appear, select Empty Application.
Figure 27.2 Creating a new iOS Application
Xcode’s project templates exist to make your job easier. They contain boilerplate code that can speed up development. However, here we intentionally choose the Empty Application template, which is as close to a blank template as you can get. Allowing Xcode to generate too much boilerplate code at this point gets in the way of your learning how things work.
In addition, the names of these templates often change with new Xcode releases, so don’t worry if the templates you see don’t exactly match Figure 27.2. Look for the simplest-sounding template and then make changes to align your code with the book’s code. If you have trouble reconciling your