2013-06-19 123 views
2

我試圖保存NSMutableDictionaryapplicationDidEnterBackgroundAppDelegate.mplist文件。在保存之後立即嘗試檢查文件是否存在並讀回,但找不到該文件。讀寫NSMutableDictionary到plist文件

NSString *photoCacheFilename = @"photoCache.plist"; 
[photoDict writeToFile:photoCacheFilename atomically:YES]; 
NSLog(@"File name: %@", photoCacheFilename); 
BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:photoCacheFilename]; 
if(isFile) 
{ 
    NSLog (@"File found"); 
    NSMutableDictionary *newDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:photoCacheFilename]; 
} 
else 
{ 
    NSLog (@"File not found"); 
} 

我修改了一些用戶建議的代碼,但仍找不到該文件。我不確定我是否正確檢查文件的存在。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsPath = [paths objectAtIndex:0]; 
NSString *photoCacheFilename = [documentsPath stringByAppendingPathComponent:@"photoCache.plist"]; 

[photoDict writeToFile:photoCacheFilename atomically:YES]; 

BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:photoCacheFilename]; 
if(isFile) 
{ 
    NSLog (@"File found"); 
    NSMutableDictionary *newDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:photoCacheFilename]; 
} 
else 
{ 
    NSLog (@"File not found"); 
} 
+0

photoCacheFilename應complate路徑不只是文件名.. – bhawesh

+0

我試過的完整路徑,如以下,但仍是同樣的結果:的NSString * photoCacheFilename = [@ 「〜/文檔/ photoCache.plist」 stringByExpandingTildeInPath]; – kzia

+0

正如他在[問題] [1]的回答中那樣獲得完整路徑。 [1]:http://stackoverflow.com/questions/9433598/write-into-plist-file-using-nsdictionary-object – keen

回答

4

你沒有指定Documents目錄的正確路徑

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsPath = [paths objectAtIndex:0]; 

NSString *photoCacheFilename = [documentsPath stringByAppendingPathComponent:@"photoCache.plist"]; // Correct path to Documents Dir in the App Sand box 

[photoDict writeToFile:photoCacheFilename atomically:YES]; //Write 
+0

查看我在編輯中的問題。 – kzia

+0

嗯,你能檢查'writeToFile'是否返回true或false嗎? –

+0

'writeToFile'失敗。它看起來像我試圖寫的字典包含不允許寫入plist文件的數據類型。謝謝您的幫助。 – kzia

1

您必須指定一個完整的路徑來保存數據:

NSString *myFile = @"myFile.plist"; 
NSArray *filePaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [filePaths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:myFile]; 
[photoDict writeToFile:path atomically:YES]; 
+0

在該問題中查看我的編輯。 – kzia

+0

對我來說似乎是正確的...嘗試實例化您的可變字典而不檢查文件的存在,在調試區域放置一個斷點並檢查其值。 – zbMax

+0

'writeToFile'失敗。它看起來像我試圖寫的字典包含不允許寫入plist文件的數據類型。謝謝您的幫助。 – kzia

0

斯威夫特3.0

獲取文檔目錄中的photoCache.plist文件路徑。

func getPath() -> String { 
     let plistFileName = "photoCache.plist" 
     let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) 
     let documentPath = paths[0] as NSString 
     let plistPath = documentPath.appendingPathComponent(plistFileName) 
     return plistPath 
    } 

要檢查文件是否存在。

let plistPath = self.getPath() 
if FileManager.default.fileExists(atPath: plistPath) { 
    //File Exists 
}