My Blog List

Android FirstAid Coding

A Small Help From a Small Heart
Powered by Blogger.

A software professional, who still likes to code, likes to blog and loves gadgets.

Wednesday 1 May 2013

How to store custom objects in NSUserDefaults in Iphone

Situation
=============

  • I am storing Custom object in NSMutableArray
  • then storing NSMutableArray into NSMutableDictionary.
  • Where Key of NSMutableDictionary is a unique_foldername(NSString) and value of NSDictionary is a NSMutableArray.
Question
==============
  • Now how can i store NSMutableDictionary in NSUserDefaults ?
  • Now how can i retrieve NSMutableDictionary from NSUserDefaults?
Solution
==============

DocumentVo.h
-----------------
#import <UIKit/UIKit.h>

@interface DocumentVo : NSObject{
    NSString *docName,*docUpdatedTime,*docImage;
    int docCount;
}
@property (nonatomic, retain) NSString *docName,*docUpdatedTime,*docImage;
@property (nonatomic) int docCount;
@end

DocumentVo.m
----------------- 
#import "DocumentVo.h"

@implementation DocumentVo
@synthesize docName,docUpdatedTime,docCount,docImage;

- (void)encodeWithCoder:(NSCoder *)encoder {
    //Encode properties, other class variables, etc
    [encoder encodeObject:self.docName forKey:@"docName"];
    [encoder encodeObject:self.docUpdatedTime forKey:@"docUpdatedTime"];
   
    [encoder encodeInt:self.docCount forKey:@"docCount"];
    [encoder encodeObject:self.docImage forKey:@"docImage"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    if((self = [super init])) {
        //decode properties, other class vars
        self.docName = [decoder decodeObjectForKey:@"docName"];
        self.docUpdatedTime = [decoder decodeObjectForKey:@"docUpdatedTime"];
        self.docCount = [decoder decodeIntForKey:@"docCount"];
        self.docImage = [decoder decodeObjectForKey:@"docImage"];
    }
    return self;
}
- (void) dealloc {
    [docName release];
    [docUpdatedTime release];
    //[docCount release];
    [docImage release];
    [super dealloc];
}

@end
 

 Saving Object
---------------------

    NSString *rootFolderName;
    NSUserDefaults *defauktCenter = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *allFolderWithImageDict=[NSMutableDictionary dictionary];
    NSMutableDictionary *resultDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:respData];
    NSMutableArray *docArray=[[NSMutableArray alloc] init];   
    DocumentVo *docVo=[[DocumentVo alloc] init];
    docVo.docCount=1;
    docVo.docName=@"FolderName";
    docVo.docUpdatedTime=@"12:23";
    docVo.docImage=@"FolderName/myfilename";
    [docArray addObject:docVo];  
  
    [allFolderWithImageDict setValue:docArray forKey:rootFolderName];  
    NSData *myEncodedObject = [NSKeyedArchiver     archivedDataWithRootObject:allFolderWithImageDict];  
    [defauktCenter setValue:myEncodedObject forKey:@"FolderImageDict"];
    [defauktCenter synchronize];


  Retrieving Object

--------------------------
    NSData *respData1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"FolderImageDict"];
    NSMutableDictionary *resultDictionary1 = [NSKeyedUnarchiver unarchiveObjectWithData:respData1];
 
    NSArray * array= [resultDictionary1 objectForKey:rootFolderName] ;
    DocumentVo *docVo1=[array objectAtIndex:0];
    NSLog(@"docVo.docCount==%d",docVo1.docCount);
    NSLog(@"docVo.docName==%@",docVo1.docName);
    NSLog(@"docVo.docUpdatedTime==%@",docVo1.docUpdatedTime);
    NSLog(@"docVo.docImage==%@",docVo1.docImage);





 

No comments:

Post a Comment