meera
meera

Reputation: 53

Saving NSDictionary with NSMutableArray as a value to NSUserDefaults

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

Upvotes: 0

Views: 1134

Answers (3)

Bhavin
Bhavin

Reputation: 27225

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 !!!

Upvotes: 2

AKB
AKB

Reputation: 122

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);

Upvotes: 0

Or Arbel
Or Arbel

Reputation: 2975

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

Upvotes: 0

Related Questions