【问题标题】:Reading and writing NSMutableDictionary to a plist file读取和写入 NSMutableDictionary 到 plist 文件
【发布时间】:2013-06-19 12:52:40
【问题描述】:

我正在尝试将NSMutableDictionary 中的applicationDidEnterBackground 中的AppDelegate.m 保存到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 应该是完整路径而不仅仅是文件名..
  • 我尝试了完整路径,如下,但结果仍然相同: NSString *photoCacheFilename = [@"~/Documents/photoCache.plist" stringByExpandingTildeInPath];
  • 获取完整路径,就像他在回答这个 [问题][1] 时所做的那样。 [1]:stackoverflow.com/questions/9433598/…
  • 查看我对问题的修改。

标签: ios plist nsmutabledictionary


【解决方案1】:

您没有指定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

【讨论】:

  • 嗯,你能检查一下writeToFile是返回true还是false?
  • writeToFile 失败。看起来我正在尝试编写的字典包含不允许写入 plist 文件的数据类型。感谢您的帮助。
【解决方案2】:

你必须指定一个完整的路径来保存数据:

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

【讨论】:

  • 对我来说似乎是正确的...尝试在不检查文件是否存在的情况下实例化可变字典,在调试区域放置断点并检查其值
  • writeToFile 失败。看起来我正在尝试编写的字典包含不允许写入 plist 文件的数据类型。感谢您的帮助。
  • 你的字典里有哪些数据?
【解决方案3】:

在 SWIFT 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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    相关资源
    最近更新 更多