Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 949
crarho Avatar asked Nov 10 '12 23:11

crarho


1 Answers

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;
}
like image 122
tillerstarr Avatar answered Nov 13 '22 03:11

tillerstarr