i’m saving/retrieving data from a plist file but i have a problem, i can’t create the plist file if it doesn’t exist. i need it to be created in the Document folder withing the app.
my code is
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];
}
i searched over to solve the problem but with no chance, i don’t know what is missing in the code.
please if someone can explain to me how to do it.
my code is in the viewDidLoad .
thanks in advance.
You are never creating the file – change this first reference to fileExists:
to this:
Now you will have created the file. Later on you are going to need to do the same thing by SAVING a full dictionary to that same file later on. IMPORTANT NOTE: test the return codes from these calls!