1

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");
}
yesimarobot
  • 283
  • 7
  • 16

3 Answers3

3

Can you mention what object is "matchedPaletteColor1Array". I believe, its some custom class object. With my knowledge the writable objects must conform to NSCoding, which NSString,NSDate etc are already, So in given code, setting "matchedPaletteColor1Array" as some string will work.

You can look at following post to make your object NSCoding enabled. objects conforming to nscoding will not writetofile

Community
  • 1
  • 1
moeen-ud-Din
  • 160
  • 1
  • 13
1

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
  • 15,018
  • 3
  • 28
  • 19
0

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

Jordan
  • 21,746
  • 10
  • 51
  • 63