Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's certainly nothing wrong with saving to NSUserDefaults, but if you want to save your properties to disk, I've put together some code for you for saving to a .plist file and then later retrieving it. You can also find it in this <a href="https://gist.github.com/4108317">gist</a>.</p> <h1>Saving</h1> <pre><code>// We're going to save the data to SavedState.plist in our app's documents directory NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *plistPath = [rootPath stringByAppendingPathComponent:@"SavedState.plist"]; // Create a dictionary to store all your data NSMutableDictionary *dataToSave = [NSMutableDictionary dictionary]; // Store any NSData, NSString, NSArray, NSDictionary, NSDate, and NSNumber directly. See "NSPropertyListSerialization Class Reference" for more information. NSString *myString = @"Hello!" [dataToSave setObject:myString forKey:@"MyString"]; // Wrap primitives in NSValue or NSNumber objects. Here are some examples: BOOL someBool = YES; NSNumber *boolValue = [NSNumber numberWithBool:someBool]; [dataToSave setObject:boolValue forKey:@"SomeBoolValue"]; int someInteger = 99; NSInteger *integerValue = [NSNumber numberWithInteger:someInteger]; [dataToSave setObject:integerValue forKey:@"SomeIntegerValue"]; // Any objects that conform to NSCoding can be archived to an NSData instance. In this example, MyClass conforms to NSCoding. MyClass *someObject = [[MyClass alloc] init]; NSData *archivedStateOfSomeObject = [NSKeyedArchiver archivedDataWithRootObject:someObject]; [dataToSave setObject:archivedStateOfSomeObject forKey:@"SomeObject"]; // Create a serialized NSData instance, which can be written to a plist, from the data we've been storing in our NSMutableDictionary NSString *errorDescription; NSData *serializedData = [NSPropertyListSerialization dataFromPropertyList:dataToSave format:NSPropertyListXMLFormat_v1_0 errorDescription:&amp;errorDescription]; if(serializedData) { // Write file NSError *error; BOOL didWrite = [serializedData writeToFile:plistPath options:NSDataWritingFileProtectionComplete error:&amp;error]; NSLog(@"Error while writing: %@", [error description]); if (didWrite) NSLog(@"File did write"); else NSLog(@"File write failed"); } else { NSLog(@"Error in creating state data dictionary: %@", errorDescription); } </code></pre> <h1>Loading</h1> <pre><code>// Fetch NSDictionary containing possible saved state NSString *errorDesc = nil; NSPropertyListFormat format; NSString *plistPath; NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; plistPath = [rootPath stringByAppendingPathComponent:@"SavedState.plist"]; NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath]; NSDictionary *unarchivedData = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&amp;format errorDescription:&amp;errorDesc]; // If NSDictionary exists, look to see if it holds a saved game state if (!unarchivedData) { NSLog(@"Error reading plist: %@, format: %d", errorDesc, format); } else { // Load property list objects directly NSString *myString = [unarchivedData objectForKey:@"MyString"]; // Load primitives NSNumber *boolValue = [unarchivedData objectForKey:@"SomeBoolValue"]; BOOL someBool = [boolValue boolValue]; NSNumber *integerValue = [unarchivedData objectForKey:@"SomeIntegerValue"]; BOOL someBool = [integerValue integerValue]; // Load your custom objects that conform to NSCoding NSData *someObjectData = [unarchivedData objectForKey:@"SomeObject"]; MyClass *someObject = [NSKeyedUnarchiver unarchiveObjectWithData:someObjectData]; } </code></pre> <p>For further reading, check out the <a href="http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Archiving/Archiving.html#//apple_ref/doc/uid/10000047i">Archives and Serialization Programming Guide</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload