我試圖保存NSMutableDictionary
在applicationDidEnterBackground
的AppDelegate.m
和plist
文件。在保存之後立即嘗試檢查文件是否存在並讀回,但找不到該文件。讀寫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");
}
photoCacheFilename應complate路徑不只是文件名.. – bhawesh
我試過的完整路徑,如以下,但仍是同樣的結果:的NSString * photoCacheFilename = [@ 「〜/文檔/ photoCache.plist」 stringByExpandingTildeInPath]; – kzia
正如他在[問題] [1]的回答中那樣獲得完整路徑。 [1]:http://stackoverflow.com/questions/9433598/write-into-plist-file-using-nsdictionary-object – keen