Online Book Reader

Home Category

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

By Root 419 0
*)tv;

// Rows can be deleted and moved

- (BOOL)tableView:(UITableView *)tv canEditRowAtIndexPath:(NSIndexPath *)ip;

- (BOOL)tableView:(UITableView *)tv canMoveRowAtIndexPath:(NSIndexPath *)ip;

- (void)tableView:(UITableView *)tv

commitEditingStyle:(UITableViewCellEditingStyle)editingStyle

forRowAtIndexPath:(NSIndexPath *)ip;

- (void)tableView:(UITableView *)tv

moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath

toIndexPath:(NSIndexPath *)destinationIndexPath;

// To save ink and paper, I'm leaving out a few optional method declarations.

@end

(Like classes, protocols have reference pages in Apple’s developer documentation, which you can search for and browse to see the methods a protocol contains.)

When you create a class to fulfill the role of UITableView's data source, you explicitly say, “This class conforms to the UITableViewDataSource protocol” in the header file. It looks like this:

@interface TerrificViewController : UIViewController

...

@end

That is, “TerrificViewController is a subclass of UIViewController and conforms to the UITableViewDataSource protocol.”

If your class conforms to several protocols, list them within the angle brackets:

@interface TerrificViewController : UIViewController

Then, in the TerrificController.m file, you need to implement the required methods in each protocol. If you forget to implement one of the required methods, you will get a stern warning from the compiler.

You will also browse through the optional methods and pick out the ones that you wish to implement. If you implement them, they will be called automatically at the appropriate time.

Final note: In the Callbacks program in Chapter 24, you made an instance of Logger the delegate of an NSURLConnection object. But you didn’t declare in Logger.h that Logger conforms to a protocol. As this is being written, there is no formal protocol for NSURLConnection delegates. I would not be surprised if this changes. (If when building Callbacks you received a warning along the lines of “This object doesn’t conform to the NSURLConnectionDelegate protocol,” this change has occurred.)

26

Property Lists


Sometimes you need a file format that can be read by both computers and people. For example, let’s say that you want to keep a description of your stock portfolio in a file. As you add new stocks, it would be nice to be able to edit that file easily by hand. But, it might also be handy for one of your programs to be able to read it. When facing this problem, most Objective-C programmers use a property list.

A property list is a combination of any of the following things:

NSArray

NSDictionary

NSString

NSData

NSDate

NSNumber (integer, float or Boolean)

For example, an array of dictionaries with string keys and date objects is a property list (or just a “P-list”).

Reading and writing a property list to a file is really easy. In Xcode, create a new project: a Foundation Command Line Tool named stockz and add the following code:

#import

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];

}

return 0;

}

(Notice that I reused the stock pointer. I used it to point to the first dictionary and then to the second.)

Figure 26.1 An array of dictionaries

When you run the program, you’ll get a file stocks.plist. If you open it in a text editor, it looks like this:

®Online Book Reader