I know there are tons of threads already on this issue, but I cannot seem to find one that solves my problem. I have a plist with a dictionary as the root, containing three arrays. My code to write to the plist works fine in the simulator but is (null) on the device.
- I'm not trying to write to the app bundle,
My file path is correct and I am checking at launch to make sure the file exists in the Documents folder (and it does exist).
- (void) writeToPlist:(NSString *)fileName playerColor:(NSString *)player withData:(NSArray *)data { NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES); NSString *documentsDirectory = [sysPaths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName]; NSLog(@"File Path: %@", filePath); NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:filePath]; NSLog(@"plist: %@", [plistDict description]); [plistDict setValue:data forKey:player]; BOOL didWriteToFile = [plistDict writeToFile:filePath atomically:YES]; if (didWriteToFile) { NSLog(@"Write to file a SUCCESS!"); } else { NSLog(@"Write to file a FAILURE!"); } }
Debug output:
File Path: /var/mobile/Applications/CA9D8884-2E92-48A5-AA73-5252873D2571/Documents/CurrentScores.plist
plist: (null)
Write to file a FAILURE!
I've used this same method in other projects, so I don't know if I'm forgetting something or what the deal is. I've checked spelling/capitalization and remade the plist, neither made any difference.
So why is plistDict (null) on the device, but not on the simulator? I apologize if I missed the solution in another post on plists.