Tom Lilletveit
Tom Lilletveit

Reputation: 2002

iOS persistence: storing and retrieving items in directory

I want to save a file to a folder, and then retrieve all the contents of that folder, so I do:

NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:storageFolder];
// StorageFolder is just a string like: "@"/FavoritesFolder"
// Filename is just a title given like "myTune.mp3"        
NSString *destinationString = [NSString stringWithFormat:@"%@/%@",dataPath,filename];
  BOOL success = [object writeToFile:destinationString atomically:YES];

Then I want to retrieve the object, so I do

NSArray *dirContents = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:dataPath error:nil];

But all I get an array of filename (like myTune.mp3). not the Object which is a NSDictionary. What am I doing wrong?

Upvotes: 0

Views: 113

Answers (1)

Wain
Wain

Reputation: 119031

You aren't strictly 'doing' anything wrong. It's your expectations that are wrong. The method you're using (contentsOfDirectoryAtPath:)

Performs a shallow search of the specified directory and returns the paths of any contained items.

If you want to load the file back into memory you need to:

  1. Decide which file you want
  2. Get the full path to the file (directory path and file name)
  3. Load the file (if it's a dictionary, dictionaryWithContentsOfFile:)

Upvotes: 1

Related Questions