Thursday, May 5, 2011
Reading and Writing plists
Reading a plist file from the application bundle just requires a few lines of code, and some error handling. I like to place that code in a nice convenience method like this:
- (id)readPlist:(NSString *)fileName {
NSData *plistData;
NSString *error;
NSPropertyListFormat format;
id plist;
NSString *localizedPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];
plistData = [NSData dataWithContentsOfFile:localizedPath];
plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
if (!plist) {
NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);
[error release];
}
return plist;
}
I’m not too fond of using id as return values or parameters to methods. I prefer stronger type checks, so I typically wrap the readPlist method in two methods that return either an array or a dictionary.
- (NSArray *)getArray:(NSString *)fileName {
return (NSArray *)[self readPlist:fileName];
}
- (NSDictionary *)getDictionary:(NSString *)fileName {
return (NSDictionary *)[self readPlist:fileName];
}
Writing to a plist file is not much more difficult:
- (void)writePlist:(id)plist fileName:(NSString *)fileName {
NSData *xmlData;
NSString *error;
NSString *localizedPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];
xmlData = [NSPropertyListSerialization dataFromPropertyList:plist format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if (xmlData) {
[xmlData writeToFile:localizedPath atomically:YES];
} else {
NSLog(@"Error writing plist to file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);
[error release];
}
}
- (id)readPlist:(NSString *)fileName {
NSData *plistData;
NSString *error;
NSPropertyListFormat format;
id plist;
NSString *localizedPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];
plistData = [NSData dataWithContentsOfFile:localizedPath];
plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
if (!plist) {
NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);
[error release];
}
return plist;
}
I’m not too fond of using id as return values or parameters to methods. I prefer stronger type checks, so I typically wrap the readPlist method in two methods that return either an array or a dictionary.
- (NSArray *)getArray:(NSString *)fileName {
return (NSArray *)[self readPlist:fileName];
}
- (NSDictionary *)getDictionary:(NSString *)fileName {
return (NSDictionary *)[self readPlist:fileName];
}
Writing to a plist file is not much more difficult:
- (void)writePlist:(id)plist fileName:(NSString *)fileName {
NSData *xmlData;
NSString *error;
NSString *localizedPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];
xmlData = [NSPropertyListSerialization dataFromPropertyList:plist format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if (xmlData) {
[xmlData writeToFile:localizedPath atomically:YES];
} else {
NSLog(@"Error writing plist to file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);
[error release];
}
}
Creating a New Navigation-Based Application
Open Up Xcode

You will be doing all of your development in Xcode. Then close the Welcome window (if it shows up)
Start a new iPhone OS Project
Click Xcode > New Project and a window should pop up like this:

Make sure Application is selected under iPhone OS and then select Navigation-Based Application. Click Choose… It will ask you to name your project. Type in “Hello World” and let’s get started.
You will be doing all of your development in Xcode. Then close the Welcome window (if it shows up)
Start a new iPhone OS Project
Click Xcode > New Project and a window should pop up like this:
Make sure Application is selected under iPhone OS and then select Navigation-Based Application. Click Choose… It will ask you to name your project. Type in “Hello World” and let’s get started.
iPhone Programming Tutorial – UITableView Hello World
In this tutorial I will walk to you through creating a simple “Hello World” application using a UITableView for the iPhone. There are many ways that a Hello World program could be made on the iPhone, I am going to show you the simplest. This tutorial assumes you have a basic understanding of Objective-C. Apple has provided a very simple and straight forward tutorial on Objective-C. You can find it here.
You will learn how to:
You will learn how to:
- Create a New Navigation-Based Application
- Learn About the Default Files
- Update the UITableView Cells to Display “Hello World” Text
Debugging Tutorial – Automating Your Tests With A UIRecorder Instrument
If you have ever experienced a bug in your application that took many steps to reproduce, then this tutorial is for you. By nature, testing and debugging are very tedious processes. This is especially the case for the iPhone.
Say you have an app that drills down 5-levels deep to some other view. Now let’s say that you have a bug on that view 5 levels deep. Your normal method of debugging is:
1. Open up the app you wish to test/debug
2. Launch it in the simulator
3. Open Instruments – This is located in /Developer/Applications/Instruments (just do a spotlight search for it)

4. Select UIRecorder and click Choose - You should now see a window like this

5. Now, we need to attach this tool to the iPhone Simulator process. Click the drop-down above
Default Target -> Attach to Process -> iPhone Simulator.

6. Now click Drive & Record and do all of the steps in the simulator to test your app. At this point, the UI Recorder is recording your every move. When you have finished press the Stop button. Note: After you have recorded your actions, don’t move the simulator as it will mess up the entire process.
7. Modify your code…
8. Press the Drive & Record button and watch the magic happen. The test is automatically done for you. You should see your mouse move over to the simulator and the system mimicking every action that you did before.
That’s it! I know this isn’t a super long tutorial (or even a coding tutorial), but the UI Recorder has sped my test time up tremendously. Give it a shot and let me know what you think. Here is a short video of me going through all of the steps.
video tutorial
Say you have an app that drills down 5-levels deep to some other view. Now let’s say that you have a bug on that view 5 levels deep. Your normal method of debugging is:
- Run the app
- Tap view 1
- Tap view 2
- Tap view 3
- Tap view 4
- Tap view 5
- (Crash)
- Change some code
- Repeat
1. Open up the app you wish to test/debug
2. Launch it in the simulator
3. Open Instruments – This is located in /Developer/Applications/Instruments (just do a spotlight search for it)
4. Select UIRecorder and click Choose - You should now see a window like this
5. Now, we need to attach this tool to the iPhone Simulator process. Click the drop-down above
Default Target -> Attach to Process -> iPhone Simulator.
6. Now click Drive & Record and do all of the steps in the simulator to test your app. At this point, the UI Recorder is recording your every move. When you have finished press the Stop button. Note: After you have recorded your actions, don’t move the simulator as it will mess up the entire process.
7. Modify your code…
8. Press the Drive & Record button and watch the magic happen. The test is automatically done for you. You should see your mouse move over to the simulator and the system mimicking every action that you did before.
That’s it! I know this isn’t a super long tutorial (or even a coding tutorial), but the UI Recorder has sped my test time up tremendously. Give it a shot and let me know what you think. Here is a short video of me going through all of the steps.
video tutorial
simple Navigationbased tutorial
It’s time to start displaying some drinks. You’ll need to make some modifications to both the RootViewController.h and RootViewController.m files
1Declare the drinks array.(in RootViewController.h )
2 Implement and populate the array.(in RootViewController.m)
In RootViewController.m, uncomment and expand the ViewDidLoad methods.
Tell the table how many rows you have.
Populate the table cells.
3 Tell the table how many rows you have.

1Declare the drinks array.(in RootViewController.h )
@interface RootViewController : UITableViewController{
NSMutableArray* drinks;
}
@property (nonatomic, retain) NSMutableArray* drinks;
@end(RootViewController.m)@synthesize drinks;
-(void)dealloc{
[drinks release];
[super dealloc];
}2 Implement and populate the array.(in RootViewController.m)
In RootViewController.m, uncomment and expand the ViewDidLoad methods.
Tell the table how many rows you have.
Populate the table cells.
- (void)viewDidLoad { [super viewDidLoad];
RootViewController.m
NSMutableArray* tmpArray = [[NSMutableArray alloc]
initWithObjects:@”Firecracker”, @”Lemon Drop”, @”Mojito”,nil];
self.drinks = tmpArray; [tmpArray release];
// Uncomment the following line to display an Edit button in
//the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self. editButtonItem;
}3 Tell the table how many rows you have.
//Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.drinks count];
4 Populate the table cells
// Configure the cell.
cell.textLabel.text = [self.drinks objectAtIndex:indexPath.row];
return cell;
}
Now you’re ready to go. Save it, build and run, and you’ll see the three drinks in your app in the main view.
Subscribe to:
Posts (Atom)
