Sometimes we have to cache some kind of information, or store it easily because we don't need to make searches on them, just we want to recover ir quickly. In this case using SQLite for local storage is a big solution for a little problem, and store the information as a XML based file lead us to write a serializer and a custom XMLParser, so the best option to accomplish that is store it in NSUserDefaults.
Maybe you have already stored basic information like NSString or NSNumber objects in user defaults, so you know that it is an easy way to store a NSObject into a NSDictionary.
But maybe you want to store a little array of information, for example a little shop list with identification number, and description, so you have created a object like this.
@interface ShopItem : NSObject
{
int numberID;
NSString *description;
}
@property (nonatomic, assign) int numberID;
@property (nonatomic, retain) NSString *description;
@end
So, how could we store a NSArray containing some of this objects into NSUserDefaults? Store the NSArray object into NSUserDefaults as usually cause our app to crash, so we have to serialize and deserialize the NSArray of custom objects implementing NSCoding protocol in our custom NSObject to encode key-value pairs for each property of our custom object.
@interface ShopItem : NSObject <NSCoding>
{
int numberID;
NSString *description;
}
@property (nonatomic, assign) int numberID;
@property (nonatomic, retain) NSString *description;
@end
@implementation ShopItem
@synthesize number, description, shoppingDate;
- (void)encodeWithCoder:(NSCoder *)coder;
{
[coder encodeObject:description forKey:@"description"];
[coder encodeInteger:numberID forKey:@"numberID"];
}
- (id)initWithCoder:(NSCoder *)coder;
{
self = [[ShopItem alloc] init];
if (self != nil)
{
description = [coder decodeObjectForKey:@"description"];
numberID = [coder decodeIntegerForKey:@"numberID"];
}
return self;
}
@end
Once we have implemented NSCoding protocol in the custom object, we are able to store our NSArray in NSUserDefaults like that
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:objectArray] forKey:@"savedArray"];
To retrieve our NSArray again, we should recover the NSArrayObject from NSUserDefaults, check that it's not nil (be careful, if you try to decode a nil array, you'll get an EXC_BAD_ACCESS exception) and unarchive it.
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"savedArray"];
if (dataRepresentingSavedArray != nil)
{
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
objectArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
else
objectArray = [[NSMutableArray alloc] init];
}
With this pieces of code, you could store and retrieve little sets of information in your NSUserDefaults local storage.
I hope this helps you.
i have inserted my custom object into array
ReplyDeletebut now i dont know how to work with this NSCoding.
I want to insert my nscoding custom object into array
How to achieve it???