0

I am storing UIImage in NSMutableArray then storing NSMutableArray into NSDictionary. Key of NSDictionary is a foldername and value of NSDictionary is a NSMutableArray. Now how can i store and retrieve NSDictionary in NSUserDefaults.

I have done as follows:

NSMutableArray *imageArray=[[NSMutableArray alloc] init];
[imageArray addObject:selectedImage];


[allFolderWithImageDict setValue:imageArray forKey:@"UniqueFolderName"];

NSUserDefaults *defauktCenter = [NSUserDefaults standardUserDefaults];
[defauktCenter setValue:allFolderWithImageDict forKey:@"FolderImageDict"];
[defauktCenter synchronize];

But NSDictionary is not saving in NSUserDefaults.

Please suggest with some example

thanks

meera
  • 53
  • 2
  • 9

3 Answers3

2

To Store and Retrieve Values of Custom Objects, you can use NSKeyedArchiver and NSKeyedUnarchiver Classes :

// To Save. . .
NSData *resData = [NSKeyedArchiver archivedDataWithRootObject:allFolderWithImageDict];
[[NSUserDefaults standardUserDefaults] setObject:resData forKey:@"FolderImageDict"];

// To Load. . .
NSData *respData = [[NSUserDefaults standardUserDefaults] objectForKey:@"FolderImageDict"];
resultDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:respData];
NSLog(@"dict :: %@",resultDictionary);

GoodLuck !!!

Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • I got the answer from this link http://rajuandroid.blogspot.in/2013/05/how-to-store-custom-objects-in.html – meera May 01 '13 at 13:32
0

To store UIImage objects in NSUserDefaults you need to a category implementation for UIImage that implements NSCoding protocol

here is one: https://code.google.com/p/iphonebits/source/browse/trunk/src/Categories/UIImage-NSCoding.h https://code.google.com/p/iphonebits/source/browse/trunk/src/Categories/UIImage-NSCoding.m

from http://iphonedevelopment.blogspot.it/2009/03/uiimage-and-nscoding.html

Or Arbel
  • 2,965
  • 2
  • 30
  • 40
0

Instead of using NSDictionary, use NSMutableDictionary.... :)

And to retrieve the data stored in dictionary.....

  NSUserDefaults *d = [NSUserDefaults standardUserDefaults];

  NSString *loadname1 = [d objectForKey:@"FolderImageDict"];

  NSLog(@"%@", loadname1);
AKB
  • 132
  • 13