views:

584

answers:

2

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");
}
A: 

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:

for (id key in paletteDictionary) 
{
    NSLog(@"key: %@, value: %@, class: %@", key, [paletteDictionary objectForKey:key], NSStringFromClass([[paletteDictionary objectForKey:key] class]));
}

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.

Alan Rogers
The UserPaletteData.plist isn't there.
yesimarobot
I also checked matchedPaletteColor1Array and it's working.
yesimarobot
Try my updated instructions above to find the culprit.
Alan Rogers
I'm only saving one array into the dictionary currently. The array only contains strings.
yesimarobot
Can you confirm that paletteDictionary is not == nil ?
Alan Rogers
A: 

Check that you are allocating (alloc/init) paletteDictionary before you use it.

Jordan