Cs193P - Lecture 9: Iphone Application Development
Cs193P - Lecture 9: Iphone Application Development
// Reading
- (id)initWithContentsOfFile:(NSString *)aPath;
- (id)initWithContentsOfURL:(NSURL *)aURL;
Writing an Array to Disk
NSArray *array = [NSArray arrayWithObjects:@“Foo”,
[NSNumber numberWithBool:YES],
[NSDate dateWithTimeIntervalSinceNow:60],
nil];
[array writeToFile:@“MyArray.plist” atomically:YES];
! Mutability
! SomeImage.png
! Documents
! Library
! Caches
! Preferences
// Documents directory
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
// <Application Home>/Documents/foo.plist
NSString *fooPath =
[documentsPath stringByAppendingPathComponent:@“foo.plist”];
Including Writable Files with Your App
• Many applications want to include some starter data
• But application bundles are code signed
! You can’t modify the contents of your app bundle
• To include a writable data file with your app...
! Build it as part of your app bundle
! On first launch, copy it to your Documents directory
Archiving Objects
Archiving Objects
• Next logical step from property lists
! Include arbitrary classes
! Complex object graphs
• Decoding an archive
NSArray *polygons = nil;
NSString *path = ...;
polygons = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
More on Archiving Objects
• “Archives and Serializations Programming Guide for Cocoa”
http://developer.apple.com/documentation/Cocoa/
Conceptual/Archiving/
The Joy of SQLite
SQLite
• Complete SQL database in an ordinary file
• Simple, compact, fast, reliable
• No server
• Great for embedded devices
! Included on the iPhone platform
“And just as you have received
SQLite for free, so also freely
give, paying the debt forward.”
D. Richard Hipp
When Not to Use SQLite
• Multi-gigabyte databases
• High concurrency (multiple writers)
• Client-server applications
• “Appropriate Uses for SQLite”
http://www.sqlite.org/whentouse.html
SQLite C API Basics
• Open the database
int sqlite3_open(const char *filename, sqlite3 **db);
// Your callback
int callback(void *context, int count,
char **values, char **columns);
• NSXMLParser
! Event-driven API: simpler but less powerful than libxml2
More on Parsing XML
• Brent Simmons, “libxml2 + xmlTextReader on Macs”
http://inessential.com/?comments=1&postid=3489
! Includes example of parsing Twitter XML!
• Big Nerd Ranch, “Parsing XML in Cocoa”
http://weblog.bignerdranch.com/?p=48
! Covers the basics of NSXMLReader
JSON
JavaScript Object Notation
• More lightweight than XML
• Looks a lot like a property list
! Arrays, dictionaries, strings, numbers
• Open source json-framework wrapper for Objective-C
What does a JSON string look like?
{
“instructor” : “Evan Doll”,
“students” : 60,
“itunes-u” : true,
“midterm-exam” : null,
“assignments” : [ “WhatATool”,
“HelloPoly”,
“Presence” ]
}
Using json-framework
• Reading a JSON string into Foundation objects
#import <JSON/JSON.h>
List Detail
Controller Controller
Don’t Do This!
Application
Delegate
Best Practices for Data Flow
• Figure out exactly what needs to be communicated
• Define input parameters for your objects
• For communicating back up the hierarchy, use loose coupling
! Define a generic interface (like delegation)
List Detail
Controller Controller
Delegates and Memory Management
• Delegates should be assigned, not retained, to avoid cycles
@property (assign) id delegate;
[super dealloc];
}
Recap
• Property lists
! Quick & easy, but limited
• Archived objects
! More flexible, but require writing a lot of code
• SQLite
! Elegant solution for many types of problems
• XML and JSON
! Low-overhead options for talking to “the cloud”
• Design your data flow thoughtfully
Questions?