The right way to save / serialize custom objects in iOS

I have a custom object, a subclass of UIImageView that contains several gestureRecognizer objects.

If I have several such objects stored in NSMutableArray , how will this array of objects be saved to disk so that it can be loaded when the user starts the application again?

I would like to load an array from disk and use objects.

+56
ios iphone
Mar 24 2018-11-11T00:
source share
4 answers

My implementation for something like this is following and works fine:

The user object (Settings) must implement the NSCoding protocol:

 -(void)encodeWithCoder:(NSCoder *)encoder{ [encoder encodeObject:self.difficulty forKey:@"difficulty"]; [encoder encodeObject:self.language forKey:@"language"]; [encoder encodeObject:self.category forKey:@"category"]; [encoder encodeObject:self.playerType forKey:@"playerType"]; } - (id)initWithCoder:(NSCoder *)decoder { if (self = [super init]) { self.difficulty = [decoder decodeObjectForKey:@"difficulty"]; self.language = [decoder decodeObjectForKey:@"language"]; self.category = [decoder decodeObjectForKey:@"category"]; self.playerType = [decoder decodeObjectForKey:@"playerType"]; } return self; } 

The following code writes the user object to the file (set.txt), and then restores it to the myArray array:

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"set.txt"]; NSMutableArray *myObject=[NSMutableArray array]; [myObject addObject:self.settings]; [NSKeyedArchiver archiveRootObject:myObject toFile:appFile]; NSMutableArray* myArray = [NSKeyedUnarchiver unarchiveObjectWithFile:appFile]; 
+116
Feb 03 '13 at 15:34
source share
 //store the array [NSKeyedArchiver archiveRootObject:myArray toFile:@"someFile"]; //load the array NSMutableArray* myArray = [NSKeyedUnarchiver unarchiveObjectWithFile:@"someFile"]; 

Official links .

Note that when the array contains custom object types, you must ensure that your type conforms to the NSCoding Protocol before it works.

+35
Mar 24 2018-11-11T00:
source share

This blog post explains how to store an array of user objects on disk using NSKeyedArchiver and read it using NSKeyedUnarchiver :

http://www.cocoabuilder.com/archive/cocoa/240775-saving-nsarray-of-custom-objects.html

Apple also has a very useful guide to this issue, the Programming and Serialization Guide for Archives .

+6
Mar 24 2018-11-11T00:
source share

I would like to share my improvements with Kostas solution if anyone needs them.

  • The class name can be used to generate the name of the text file to store the object.
  • This is a good solution for saving view controller objects in viewWillDisappear mode and restoring them in the viewDidLoad method.
  • The file name must be generated by a separate method to avoid code duplication.
  • After restoring the object, you should check that it is not equal to zero.



 - (void)viewDidLoad { [super viewDidLoad]; // Restoring form object from the file NSString *formFilePath = [self formFilePath]; RRCreateResumeForm *form = [NSKeyedUnarchiver unarchiveObjectWithFile:formFilePath]; if (form != nil) { self.formController.form = form; } } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; // Saving the form object to the file NSString *formFilePath = [self formFilePath]; [NSKeyedArchiver archiveRootObject:self.formController.form toFile:formFilePath]; } // Returns a file path to the file with stored form data for form controller - (NSString *)formFilePath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *formClassName = NSStringFromClass( [self.formController.form class] ); NSString *formFileName = [NSString stringWithFormat:@"%@.txt", formClassName]; NSString *formFilePath = [documentsDirectory stringByAppendingPathComponent:formFileName]; return formFilePath; } 
+2
Jul 30 '14 at 10:52
source share



All Articles