文档介绍:CS193P - Lecture 9
iPhone Application Development
Data in Your iPhone App
Chris Marcellino
Tuesday, February 2, 2010 1
Today’s Topics
• Data in Your iPhone App
■ Saving & loading local data
■ Accessing remote data over the
Tuesday, February 2, 2010 2
Today’s Topics
• Property Lists, NSUserDefaults and Settings
• iPhone’s File System
• Archiving Objects
• The Joy of SQLite
• JSON
• Apple Push Notification Service
Tuesday, February 2, 2010 3
Property Lists
Tuesday, February 2, 2010 4
Property Lists
• Convenient way to store a small amount of data
■ Arrays, dictionaries, strings, numbers, dates, raw data
■ Human-readable XML or binary format
• NSUserDefaults class uses property lists under the hood
Tuesday, February 2, 2010 5
When Not to Use Property Lists
• More than a few hundred KB of data
■ Loading a property list is all-or-nothing
• Complex object graphs
• Custom object types
• Multiple writers (. not ACID)
Tuesday, February 2, 2010 6
Reading & Writing Property Lists
• NSArray and NSDictionary convenience methods
• Operate recursively
// Writing
- (BOOL)writeToFile:(NSString *)aPath atomically:(BOOL)flag;
- (BOOL)writeToURL:(NSURL *)aURL atomically:(BOOL)flag;
// Reading
- (id)initWithContentsOfFile:(NSString *)aPath;
- (id)initWithContentsOfURL:(NSURL *)aURL;
Tuesday, February 2, 2010 7
Writing an Array to Disk
NSArray *array = [NSArray arrayWithObjects:@“Foo”,
[NSNumber numberWithBool:YES],
[NSDate dateWithTimeIntervalSinceNow:60],
nil];
[array writeToFile:@“” atomically:YES];
<?xml version="" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST //EN"
"s/PropertyList-">
<plist version="">
<array>
<string>Foo</string>
<true/>
<date>2010-02-02T09:26:18Z</date>
</array>
</plist>
Tuesday, February 2, 2010 8
Writing a Dictionary to Disk
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@“Bob”, @“Name”,