0

我想将文件保存到文件夹,然后检索该文件夹的所有内容,所以我这样做:

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];

然后我想检索对象,所以我这样做

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

但我得到的只是一个数组filename(比如 myTune.mp3)。不是Object哪个是 NSDictionary。我究竟做错了什么?

4

1 回答 1

1

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:)
于 2013-06-23T15:21:57.703 回答