What is the best way to store NSMutableArray in NSUserDefaults?

I have a custom object called Occasion, which is defined as follows:

#import <Foundation/Foundation.h> @interface Occasion : NSObject { NSString *_title; NSDate *_date; NSString *_imagePath; } @property (nonatomic, retain) NSString *title; @property (nonatomic, retain) NSDate *date; @property (nonatomic, retain) NSString *imagePath; 

Now I have NSMutableArray Occasions, which I want to save in NSUserDefaults. I know that this is not possible in a direct way, so I wonder which one is the easiest way? If serialization is the answer, then how? Because I read the documents, but could not understand how it works completely.

+4
source share
3 answers

You should use something like NSKeyedArchiver to serialize the array into NSData , save it in NSUserDefaults , and then use NSKeyedUnarchiver for subsequent deserialization:

 NSData *serialized = [NSKeyedArchiver archivedDataWithRootObject:myArray]; [[NSUserDefaults standardUserDefaults] setObject:serialized forKey:@"myKey"]; //... NSData *serialized = [[NSUserDefaults standardUserDefaults] objectForKey:@"myKey"]; NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithData:serialized]; 

You will need to implement the NSCoding protocol in your Occasion class and properly save the various properties to make this work correctly. See the Programming and Serialization Guide for Archives for more information. There should not be several lines of code for this. Sort of:

 - (void)encodeWithCoder:(NSCoder *)coder { [super encodeWithCoder:coder]; [coder encodeObject:_title forKey:@"_title"]; [coder encodeObject:_date forKey:@"_date"]; [coder encodeObject:_imagePath forKey:@"_imagePath"]; } - (id)initWithCoder:(NSCoder *)coder { self = [super initWithCoder:coder]; _title = [[coder decodeObjectForKey:@"_title"] retain]; _date = [[coder decodeObjectForKey:@"_date"] retain]; _imagePath = [[coder decodeObjectForKey:@"_imagePath"] retain]; return self; } 
+11
source

NSUserDefaults is for user settings, not for storing application data. Use CoreData or serialize objects into a document directory. To do this, you need your class to implement the NSCoding protocol.

1) Add NSCoding to Occasion.h

 @interface Occasion : NSObject <NSCoding> 

2) Implement the protocol in Occasion.m

 - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super init]) { self.title = [aDecoder decodeObjectForKey:@"title"]; self.date = [aDecoder decodeObjectForKey:@"date"]; self.imagePath = [aDecoder decodeObjectForKey:@"imagePath"]; } return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:title forKey:@"title"]; [aCoder encodeObject:date forKey:@"date"]; [aCoder encodeObject:imagePath forKey:@"imagePath"]; } 

3) Archive the data to a file in the document directory

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsPath = [paths objectAtIndex:0]; NSString *path= [documentsPath stringByAppendingPathComponent:@"occasions"]; [NSKeyedArchiver archiveRootObject:occasions toFile:path]; 

4) To unlock ...

 NSMutableArray *occasions = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; 
+3
source

You can implement NSCoding in Occasion .

Then you use [NSKeyedArchiver archivedDataWithRootObject:myArray] to create the NSData object from the array. You can put this in the user's default settings.

+1
source

All Articles