I'm developing with a team, using macs that were all imaged exactly the same. Using the machine on which I wrote the code, I can programmatically create and then write to a pList file. But when I use my home machine (same image) or when my teammates test the code on their machines, we crash with a [NSFileManager copyItemAtPath:toPath:error:]: source path is nil' error. In debugging, the filepath is valid. The bundle, however, in:
NSString *bundle = [[NSBundle mainBundle]pathForResource:@"Family" ofType:@"plist"];
[pListfileMgr copyItemAtPath:bundle toPath: pListpath error:&error];
is nil. We are using git to version control our code. We have been keeping .DS_Store and /userdata/ files out of the repo, unless I've missed something. What is going on? Here is the code, reproduced almost verbatim from here:
NSError *error;
NSArray *pListpaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *pListdocumentsDirectory = [pListpaths objectAtIndex:0];
NSString *pListpath = [pListdocumentsDirectory stringByAppendingPathComponent:@"Family.plist"];
NSFileManager *pListfileMgr = [NSFileManager defaultManager];
//Create a plist if it doesn't alread exist
if (![pListfileMgr fileExistsAtPath: pListpath])
{
NSString *bundle = [[NSBundle mainBundle]pathForResource:@"Family" ofType:@"plist"];
[pListfileMgr copyItemAtPath:bundle toPath: pListpath error:&error];
}
NSMutableDictionary *thePList = [[NSMutableDictionary alloc] initWithContentsOfFile: pListpath];
[thePList setObject:[NSString stringWithFormat:@"%@", numberOfPeopleInFamilyOfOrigin] forKey:@"famSize"];
[thePList writeToFile: pListpath atomically: YES];
I believe you are misinterpreting the intent of the code you've listed above.
The following block is checking for the existence of a file located at path pListpath
. If a file does not exist at pListpath
, it copies a file from within your bundle to pListpath
.
//Create a plist if it doesn't alread exist
if (![pListfileMgr fileExistsAtPath: pListpath])
{
NSString *bundle = [[NSBundle mainBundle]pathForResource:@"Family" ofType:@"plist"];
[pListfileMgr copyItemAtPath:bundle toPath: pListpath error:&error];
}
That is to say, you must ship a file called Family.plist
within your bundle, the intent being that this file would act as a default plist if the a plist does not already exist at path pListpath
.