2013-06-23 58 views
0

我想将文件保存到一个文件夹,然后检索该文件夹中的所有内容检索项目,所以我做的:iOS的持久性:存储和目录

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。我究竟做错了什么?

+2

备注 - 不要使用'stringWithFormat:'来建立你的路径。这样做:'NSString * destinationString = [dataPath stringByAppendingPathComponent:filename];' – rmaddy

回答

1

你并不严格“做”任何错误。你的期望是错误的。您正在使用(contentsOfDirectoryAtPath:

该方法执行一浅层搜索指定目录下,并返回的任何路径包含的项目。

如果要加载的文件放回内存,你需要:

  1. 决定要
  2. 获得完整的文件路径(目录路径和文件名)
  3. 哪个文件
  4. 加载文件(如果它是字典,dictionaryWithContentsOfFile:
+0

所以,如果我想获得一个目录的所有项目最好的方法是使用(contentsOfDirectoryAtPath :)然后我自己写一个方法使用该数组返回一个目录中所有对象的数组? –

+0

好的,所以你的目标是将所有写入该目录的字典都回存到内存中。那么是的,你需要遍历它们并加载(按照上面的)每个字典。 – Wain