Please consider subscribing to Mobile Developer Tips RSS feed or following us on Twitter
|
|
|
With just a few lines of code, you can read/write collections to/from files. The code below shows examples for writing and reading back both arrays and dictionaries.
Read and Write Collections to File
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]);
}
Verify the Output
The output in the console window for the above looks as follows:

Related posts:
- Read and Write User Preferences
- Fast Enumeration on the iPhone
- Reading a plist into an NSArray
Comments
5 Responses to “Read and Write Array, Dictionary and Other Collections to Files”
Leave Comment
Interesting, I was going to play with something similar in the next couple of weeks.
Q: If I now want to update the Array or Dictionary, would I instead use Mutable objects and simply call “writeToFile: xxxxxPath atomically:YES” (where xxxxx is array or dict)?
Thanks.
[Reply]
John Muchow Reply:
February 17th, 2010 at 2:13 pm
Hey Bill,
If I understand your question, yes, if you use mutable classes, I would think you could read into those objects, make changes, then write those same objects back to the system.
[Reply]
Hi Bill,
I’m using code similar to what you’ve written above. However, writeToFile returns NO when I try to write an array to a file, and the file remains empty.
I’ve used [[NSFileManager defaultManager] isWritableFileAtPath:locFilePath] to verify if the file exists, and is writable, which it is. I can also see the file in the file system using Finder. It is currently empty.
The array I’m trying to write is an array of dictionary objects.
Do you know if there’s a way I can troubleshoot this?
Many thanks,
Stephen C
[Reply]
@Stephen, you are correct.
Even i faced same problem when i tried to write the NSMutableArray A into plist using
I had A consist of 3 Custom Objects,
Each Object containing five NSString elements, so in all 15 NSString element.
[<A writeToFile:path atomically:YES];
But, this wont work as NSMutableArray or NSDictionary, can be written to Plist, but not the Custom NSMutableArray or NSDictionary into plist.
So, what i did is i fetched the first or desired element from the Custom Array and unwrap the object and store its corresponding five values in some temp array.
And that did solved my problem.
[Reply]
Thanks for that.
I can’t remember what I changed to get it working, but it’s been fine for a while now.
Cheers,
Stephen
[Reply]