0
我从plist文件中保存/检索数据,但是我遇到了问题,如果它不存在,我无法创建plist文件。我需要它在应用程序的“文档”文件夹中创建。 我的代码是在Xcode中创建plist文件
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"playersnames.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"playersnames.plist"] ];
}
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSFileManager *fileManager2 = [NSFileManager defaultManager];
//NSMutableDictionary *data;
if ([fileManager2 fileExistsAtPath: path])
{
data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
}
else
{
// If the file doesn’t exist, create an empty dictionary
data = [[NSMutableDictionary alloc] init];
}
我搜索到解决问题,但没有机会,我不知道是什么在代码中失踪。
如果有人能向我解释如何去做。
我的代码在viewDidLoad中。
在此先感谢。
谢谢,对我很好:) – aLFaRSi 2012-08-10 01:19:17