I have one dictionary I need to save into a plist. The paletteDictionary always returns nil:
- (void)saveUserPalette:(id) sender
{
[paletteDictionary setObject:matchedPaletteColor1Array forKey:@"1"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"UserPaletteData.plist"];
// write plist to disk
[paletteDictionary writeToFile:path atomically:YES];
}
I’m reading the data back in a different view like:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"UserPaletteData.plist"];
NSMutableDictionary *plistDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:path];
if(plistDictionary==nil ){
NSLog(@"failed to retrieve dictionary from disk");
}
Run your application in the simulator and browse to the following path:
~/Library/Application Support/iPhone Simulator/User/Applications/{A GUID}/Documents
Where A GUID will be a string like this: 06430A38-AFAC-4C68-8F39-DBD6C81A5AA6 (it’s probably the Last Modified folder).
Verify that UserPaletteData.plist is present and load it up in Property List Editor.app to verify that it contains some data.
Also make sure that the you use only the following data types in your dictionary otherwise it will fail to write to a plist: (NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary).
To verify that the dictionary you are attempting to save to disk is valid for a plist, try the following:
That should tell you if any of your objects are the wrong data type for a plist.
If that is still not helping, then you should make sure that paletteDictionary has been alloc/init’ed before saveUserPalette is called.