Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将NSMutableDictionary读写到plist文件_Ios_Plist_Nsmutabledictionary - Fatal编程技术网

Ios 将NSMutableDictionary读写到plist文件

Ios 将NSMutableDictionary读写到plist文件,ios,plist,nsmutabledictionary,Ios,Plist,Nsmutabledictionary,我正在尝试将AppDelegate.m的applicationidenterbackground中的NSMutableDictionary保存到plist文件中。保存后,我立即尝试检查文件是否存在并将其读回,但找不到该文件 NSString *photoCacheFilename = @"photoCache.plist"; [photoDict writeToFile:photoCacheFilename atomically:YES]; NSLog(@"File name: %@", pho

我正在尝试将
AppDelegate.m的
applicationidenterbackground
中的
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");
}

必须指定完整的路径才能保存数据:

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

您没有指定
文档
目录的正确路径

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
在SWIFT 3.0中

获取文档目录中的photoCache.plist文件路径。

检查文件是否存在。


photoCacheFilename应该是完整路径而不仅仅是文件名..我尝试了完整路径,如下所示,但结果仍然相同:NSString*photoCacheFilename=[@“~/Documents/photoCache.plist”stringByExpandingTildeInPath];按照他在回答[问题][1]时所做的步骤获取完整路径。[1] :查看问题中我的编辑。对我来说似乎正确。。。尝试在不检查文件存在的情况下实例化可变字典,在调试区域设置断点并检查其值
writeToFile
失败。我试图编写的字典似乎包含不允许写入plist文件的数据类型。谢谢你的帮助。你的字典里有什么样的数据?嗯,你能检查一下
writeToFile
是否返回
true
false
writeToFile
失败了。我试图编写的字典似乎包含不允许写入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
}