2013-06-23 63 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