NSArray *array = [NSArray arrayWithObjects:
@"Hefeweizen", @"IPA", @"Pilsner", @"Stout", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
array, @"array", @"Stout", @"dark", @"Hefeweizen", @"wheat", @"IPA",
@"hoppy", nil];
// Get path to documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
if ([paths count] > 0)
{
// Path to save array data
NSString *arrayPath = [[paths objectAtIndex:0]
stringByAppendingPathComponent:@"array.out"];
// Path to save dictionary
NSString *dictPath = [[paths objectAtIndex:0]
stringByAppendingPathComponent:@"dict.out"];
// Write array
[array writeToFile:arrayPath atomically:YES];
// Write dictionary
[dictionary writeToFile:dictPath atomically:YES];
// Read both back in new collections
NSArray *arrayFromFile = [NSArray arrayWithContentsOfFile:arrayPath];
NSDictionary *dictFromFile = [NSDictionary dictionaryWithContentsOfFile:dictPath];
for (NSString *element in arrayFromFile)
NSLog(@"Beer: %@", element);
for (NSString *key in dictFromFile)
NSLog(@"%@ Style: %@", key, [dictionary valueForKey:key]);
}