본문 바로가기

PDA&Mobile

아이폰 앱 개발 팁(8) : Stanford Lecture #9

* 본 포스트는 Blog.MissFlash.com에서 작성한 것으로, 원문 저작자의 동의없이 마음대로 퍼가실 수 없습니다. 포스트의 내용이 마음에 드시면 링크를 이용해주시면 감사하겠습니다.

> Stanford Lecture #9

* Reading property lists
- (id)initWithContentsOfFile:(NSString *)aPath;
- (id)initWithContentsOfURL:(NSURL *)aURL;

* Writing property lists
- (BOOL)writeToFile:(NSString *)aPath atomically:(BOOL)flag;
- (BOOL)writeToURL:(NSURL *)aURL atomically:(BOOL)flag;

* Writing an array to disk
NSArray *array = [NSArray arrayWithObjects:@"Foo", [NSNumber numberWithBool:YES], [NSDate dateWithTimeIntervalSinceNow:60], nil];
[array writeToFile:@"MyArray.plist" atomically:YES];

* Writing a dictionary to disk
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Name", @"Evan", @"Lecture", [NSNumber numberWithInt:9], nil];
[dict writeToFile:@"MyDict.plist" atomically:YES];

* Property list to NSData
+ (NSData *)dataFromPropertyList:(id)plist format:(NSPropertyListFormat)format errorDescription:(NSString *)errorString;

* NSData to property list
+ (id)propertyListFromData:(NSData *)data mutabilityOption:(NSPropertyListMutabilityOptions)opt format:(NSPropertyListFormat *)format errorDescription:(NSString *)errorString;



* Basic directories
NSString *homePath = NSHomeDirectory();
NSString *tmpPath = NSTemporaryDirectory();

* Documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];

* <Application Home>/Documents/foo.plist
NSString *fooPath = [documentsPath stringByAppendingPathComponent:@"foo.plist"];



* Encode an object for an archive
- (void)encodeWithCoder:(NSCoder *)coder{
[super encodeWithCoder:coder];
[coder encodeObject:name forKey:@"Name"];
[coder encodeInteger:numberOfSides forKey:@"Sides"];
}

* Decode an object from an archive
- (id)initWithCoder:(NSCoder *)coder{
self = [super initWithCoder:coder];
name = [[coder decodeObjectForKey:@"Name"] retain];
numberOfSides = [coder decodeIntegerForKey:@"Sides"];
}

* Creating an archive
NSArray *polygons = ...;
NSString *path = ...;
BOOL result = [NSKeyedArchiver archiveRootObject:polygons toFile:path];

* Decoding an archive
NSArray *polygons = nil;
NSString *path = ...;
polygons = [NSKeyedUnarchiver unarchiveObjectWithFile:path];



* Open the database
int sqlite3_open(const char *filename, sqlite3 **db);

* Execute a SQL statement
int sqlite3_exec(sqlite3 *db, const char *sql, int (*callback)(void*, int, char**, char**), void *context, char **error);

* Close the database
int sqlite3_close(sqlite3 *db);



* Options for parsing XML
libxml2 vs. NSXMLParser

* Reading a JSON string into Foundation objects
#import <JSON/JSON.h>
NSString *jsonString = ...;
id object = [jsonString JSONValue];

* Writing a JSON string from Foundation objects
NSDictionary *dictionary = ...;
jsonString = [dictionary JSONRepresentation];