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
}