How to archive an NSArray of custom objects to file in Objective-C - objective-c

Can you show me the syntax or any sample programs to archive an NSArray of custom objects in Objective-C?

Check out NSUserDefaults.
For Archiving your array, you can use the following code:
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:myArray] forKey:#"mySavedArray"];
And then for loading the custom objects in the array you can use this code:
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *savedArray = [currentDefaults objectForKey:#"mySavedArray"];
if (savedArray != nil)
{
NSArray *oldArray = [NSKeyedUnarchiver unarchiveObjectWithData:savedArray];
if (oldArray != nil) {
customObjectArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
} else {
customObjectArray = [[NSMutableArray alloc] init];
}
}
Make sure you check that the data returned from the user defaults is not nil, because that may crash your app.
The other thing you will need to do is to make your custom object to comply to the NSCoder protocol. You could do this using the -(void)encodeWithCoder:(NSCoder *)coder and -(id)initWithCoder:(NSCoder *)coder methods.

If you want to save to a file (rather than using NSUserDefaults) you can use -initWithContentsOfFile: to load, and -writeToFile:atomically: to save, using NSArrays.
Example:
- (NSArray *)loadMyArray
{
NSArray *arr = [NSArray arrayWithContentsOfFile:
[NSString stringWithFormat:#"%#/myArrayFile", NSHomeDirectory()]];
return arr;
}
// returns success flag
- (BOOL)saveMyArray:(NSArray *)myArray
{
BOOL success = [myArray writeToFile:
[NSString stringWithFormat:#"%#/myArrayFile", NSHomeDirectory()]];
return success;
}
There's a lot of examples on various ways to do this here: http://www.cocoacast.com/?q=node/167

Related

How can I save an Objective-C object that's not a property list object or is there a better way for this than a property list?

I'm writing a Cookbook application, and I've not been able to find anything on how to save the data of a class I've created (the Recipe class). The only way I've seen would be to possibly save the contents of this class as a whole without individually saving every element of the class for each object by making this method for my Recipe class:
-(void) writeToFile:(NSString *)file atomically:(BOOL)atomic{
}
But I have absolutely no idea how I'd go about implementing this to save this object to a file using this method. Some of the properties are:
NSString* name;
UIImage* recipePicture;
NSDate* dateAdded;
NSMutableArray* ingredients; //The contents are all NSStrings.
Does anyone know how to go about saving an object of the Recipe class?
It's been driving me crazy not being able to figure it out. Any help would be greatly appreciated. I already have a .plist entitled "RecipeData.plist".
Would I just need to write every property to the plist and initialize a new object of recipe with those properties at run time?
Adopt:
#interface Recipe : NSObject<NSCoding>
Implement:
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:name_ forKey:#"name"];
[coder encodeObject:recipePicture_ forKey:#"recipePicture"];
[coder encodeObject:dateAdded_ forKey:#"dateAdded"];
[coder encodeObject:ingredients_ forKey:#"ingredients"];
}
// Decode an object from an archive
- (id)initWithCoder:(NSCoder *)coder
{
self = [super init];
if (self!=NULL)
{
name_ = [coder decodeObjectForKey:#"name"];
recipePicture_ = [coder decodeObjectForKey:#"recipePicture"];
dateAdded_ = [coder decodeObjectForKey:#"dateAdded"];
ingredients_ = [coder decodeObjectForKey:#"ingredients"];
}
return self;
}
Now in your save:
- (void) save:(NSString*)path recipe:(Recipe*)recipe
{
NSMutableData* data=[[NSMutableData alloc] init];
if (data)
{
NSKeyedArchiver* archiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
if (archiver)
{
[archiver encodeInt:1 forKey:#"Version"];
[archiver encodeObject:recipe forKey:#"Recipe"];
[archiver finishEncoding];
[data writeToFile:path atomically:YES];
}
}
}
And in the load:
- (Recipe*) load:(NSString*)path
{
Recipe* ret=NULL;
NSData* data=[NSData dataWithContentsOfFile:path];
if (data)
{
NSKeyedUnarchiver* unarchiver=[[NSKeyedUnarchiver alloc] initForReadingWithData:data];
if (unarchiver)
{
int version=[unarchiver decodeIntForKey:#"Version"];
if (version==1)
{
ret=(Recipe*)[unarchiver decodeObjectForKey:#"Recipe"];
}
[unarchiver finishDecoding];
}
}
return ret;
}
One option (besides encoding/decoding) is to store each attribute of your class in a dictionary. Then you write the dictionary to the file. The trick is to ensure that every object you put in the dictionary is allowed in a plist. Of the four properties you show, all but the UIImage can be stored as-is.
-(BOOL)writeToFile:(NSString *)file atomically:(BOOL)atomic{
NSMutableDictionary *data = [NSMutableDictionary dictionary];
[data setObject:name forKey:#"name"];
[data setObject:dateAdded forKey#"dataAdded"];
NSDate *imageData = UIImagePNGRepresentation(recipePicture);
[data setObject:imageData forKey:#"recipePicture"];
// add the rest
return [data writeToFile:file atomically:YES];
}
I updated this to return a BOOL. If it fails, it means one of two things:
The file was inappropriate
You tried to save a non-plist friendly object in the dictonary
You need to add code to avoid trying to add nil objects if you have any. The important thing is to ensure that all keys are strings and only plist-friendly objects are stored (NSString, NSNumber, NSDate, NSValue, NSData, NSArray, and NSDictionary).

adding objects crash in ios

i am trying to add an object into a nsuserdefault, but i get this crash
"[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object"
its crashing on this line:
[currentFav addObject:incomingBabe];
I have no idea why its crashing, its working on my other project.
here is my code
-(IBAction)favorite {
NSUserDefaults *standardDefault = [NSUserDefaults standardUserDefaults];
NSMutableArray *currentFav = [[NSUserDefaults standardUserDefaults]objectForKey:#"fav"];
NSLog(#"strings stored = %#",currentFav);
NSMutableArray *newFav = [NSMutableArray arrayWithObject:[NSString stringWithFormat:#"bikini%02d.jpeg",self.currentNumber]];
if (currentFav == NULL){
currentFav = [[NSMutableArray alloc]init];
}
for(NSString *incomingBabe in newFav){
BOOL hasStringAlready = NO;
for(NSString *currentFavorite in currentFav){
if([currentFavorite isEqualToString:incomingBabe]){
hasStringAlready = YES;
NSLog(#"has string already");
break;
}
}
if (!hasStringAlready) {
[currentFav addObject:incomingBabe];
hasStringAlready = YES;
}
}
[standardDefault setObject:currentFav forKey:#"fav"];
[standardDefault synchronize];
}
Basically it says you are trying to use a method from NSMutableArray on NSArray.
This is because "Values returned from NSUserDefaults are immutable, even if you set a mutable object as the value."
NSMutableArray *currentFav = [[NSUserDefaults standardUserDefaults]objectForKey:#"fav"];
will return an array, not mutable array. You should make a mutable copy of it.
NSMutableArray *currentFav = [[[NSUserDefaults standardUserDefaults]objectForKey:#"fav"] mutableCopy];
You can get the reason of your problem from 1 floor. You can use his method to solve your problem, or like this:
NSMutableArray *currentFav = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:#"fav"]];

Storing Custom Object in NSMutableDictionary

I am trying to store a custom object in NSMutableDictionary. After saving when I read the object from NSMutableDictionary it's always null.
Here is the code
//Saving
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
CustomObject *obj1 = [[CustomObject alloc] init];
obj1.property1 = #"My First Property";
[dict setObject:obj1 forKey:#"FirstObjectKey"];
[dict writeToFile:[self dataFilePath] atomically:YES];
// Reading
NSString *filePath = [self dataFilePath];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
CustomObject *tempObj = [dict objectForKey:#"FirstObjectKey"];
NSLog(#"Object %#", tempObj);
NSLog(#"property1:%#,,tempObj.property1);
How can I store a custom class object in NSMutableDictionary?
The problem is not with putting the object into the dictionary; the problem is with writing it to a file.
Your custom class has to be serializable. You need to implement the NSCoding protocol so that Cocoa knows what to do with your class when you ask for it to be written out to disk.
This is pretty simple to do; you need to implement two methods that will look something like the following:
- (id)initWithCoder:(NSCoder *)coder {
self = [super init];
// If inheriting from a class that implements initWithCoder:
// self = [super initWithCoder:coder];
myFirstIvar = [[coder decodeObjectForKey:#"myFirstIvar] retain];
mySecondIvar = [[coder decodeObjectForKey:#"mySecondIvar] retain];
// etc.
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder {
// If inheriting from a class that implements encodeWithCoder:
// [super encodeWithCoder:coder];
[coder encodeObject:myFirstIvar forKey:#"myFirstIvar"];
[coder encodeObject:mySecondIvar forKey:#"mySecondIvar"];
// etc.
}
Essentially you're just listing the ivars that you need to save, and then reading them back in properly.
UPDATE: As mentioned by Eimantas, you'll also need NSKeyedArchiver. To save:
NSData * myData = [NSKeyedArchiver archivedDataWithRootObject:myDict];
BOOL result = [myData writeToFile:[self dataFilePath] atomically:YES];
To reload:
NSData * myData = [NSData dataWithContentsOfFile:[self dataFilePath]];
NSDictionary * myDict = [NSKeyedUnarchiver unarchiveObjectWithData:myData];
I think that should do it.
writeToFile method can store only standard types of objects into plist. If you have custom object you'd have to use NSKeyedArchiver/NSKeyedUnarchiver for this.

save and restore an array of custom objects

I have an NSArray of custom objects that I want to save and restore. Can this be done with NSUserDefaults?
You can still use NSUserDefaults if you archive your array into NSData.
For Archiving your array, you can use the following code:
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:myArray] forKey:#"mySavedArray"];
And then for loading the custom objects in the array you can use this code:
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *savedArray = [currentDefaults objectForKey:#"mySavedArray"];
if (savedArray != nil)
{
NSArray *oldArray = [NSKeyedUnarchiver unarchiveObjectWithData:savedArray];
if (oldArray != nil) {
customObjectArray = [[NSMutableArray alloc] initWithArray:oldArray];
} else {
customObjectArray = [[NSMutableArray alloc] init];
}
}
Make sure you check that the data returned from the user defaults is not nil, because that may crash your app.
The other thing you will need to do is to make your custom object comply to the NSCoder protocol. You could do this using the -(void)encodeWithCoder:(NSCoder *)coder and -(id)initWithCoder:(NSCoder *)coder methods.
EDIT.
Here's an example of what you might put in the -(void)encodeWithCoder:(NSCoder *)coder and -(id)initWithCoder:(NSCoder *)coder methods.
- (void)encodeWithCoder:(NSCoder *)coder;
{
[coder encodeObject:aLabel forKey:#"label"];
[coder encodeInteger:aNumberID forKey:#"numberID"];
}
- (id)initWithCoder:(NSCoder *)coder;
{
self = [[CustomObject alloc] init];
if (self != nil)
{
aLabel = [coder decodeObjectForKey:#"label"];
aNumberID = [coder decodeIntegerForKey:#"numberID"];
}
return self;
}
NSUserDefaults cannot write custom objects to file, only ones it knows about (NSArray, NSDictionary, NSString, NSData, NSNumber, and NSDate). Instead, you should take a look at the Archives and Serializations Programming Guide, as well as the NSCoding Protocol Reference, if you're looking to save and restore custom objects to disk. Implementing the protocol is not terribly difficult, and requires very little work.
Custom objects, no. NSUserDefaults only knows about a few basic types (NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary).
Could you use JSON (http://code.google.com/p/json-framework/) to convert your custom object to a string representation, then save an array of those to Defaults? (Using the setObject:forKey: method).
Otherwise, you could look at using sqlite, NSCoder, or even resort to fopen.

NSKeyedUnarchiver objects getting broken?

I have an array that I'm saving to NSUserDefaults, containing an array of my custom class Assignment, which conforms to the NSCoding protocol. The array saves and loads properly, and I can verify that the retrieved first object of the array is of the class Assignment. The problem happens when I try to access ivars of the Assignment object in the array. It crashes and I get the following error:
*** -[CFString respondsToSelector:]: message sent to deallocated instance 0x3948d60
Here is the code I'm using to save to user defaults. Note that I am also retrieving and checking the saved object for debugging purposes.
-(void)saveToUserDefaults:(NSArray*)myArray
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject:myArray] forKey:#"Assignments"];
[standardUserDefaults synchronize];
}
NSLog(#"Assignments array saved. (%d assignments in array)",[myArray count]);
NSData *dataCheck = [[NSData alloc] initWithData:[standardUserDefaults objectForKey:#"Assignments"]];
NSArray *arrayCheck = [[NSArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:dataCheck]];
NSLog(#"Checking saved array (%d assignments in array)",[arrayCheck count]);
if ([[arrayCheck objectAtIndex:0] isKindOfClass:[Assignment class]]) {
NSLog(#"It's of the class Assignment.");
}
Assignment *testAssignment = [[Assignment alloc] initWithAssignment:[arrayCheck objectAtIndex:0]];
NSLog(#"Title: %# Course: %#",[testAssignment title],[testAssignment course]);
}
Everything is fine until I allocate testAssignment, which is where the crash happens. Does anyone have any ideas?
EDIT: Here are my NSCoding methods in the Assignment class:
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:title forKey:#"title"];
[coder encodeObject:course forKey:#"course"];
[coder encodeObject:dueDate forKey:#"dueDate"];
[coder encodeObject:notes forKey:#"notes"];
}
- (id)initWithCoder:(NSCoder *)coder {
self = [[Assignment alloc] init];
if (self != nil)
{
title = [coder decodeObjectForKey:#"title"];
course = [coder decodeObjectForKey:#"course"];
dueDate = [coder decodeObjectForKey:#"dueDate"];
notes = [coder decodeObjectForKey:#"notes"];
}
return self;
}
Answered my own question. In initWithCoder, I needed to retain all of the objects I was decoding:
//Example
title = [[coder decodeObjectForKey:#"title"] retain];
Everything works beautifully now. :)