Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9037117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:10:08+00:00 2026-06-16T09:10:08+00:00

I’m making a Cookbook application for the iPad and iPod, and I have an

  • 0

I’m making a Cookbook application for the iPad and iPod, and I have an array of my Recipe class in my Cookbook class.

@interface Cookbook : NSObject<NSCoding>{
NSMutableArray* recipes;
}

That’s in my Cookbook class, and in my recipe class I have this:

@interface Recipe : NSObject<NSCoding>{
NSString* name;
NSMutableArray* ingredients; //List of ingredients for the recipe
UIImage* recipePicture;
NSMutableArray* instructions;
unsigned int prepTime;//in seconds
NSDate* dateAdded;
}

(I actually have more variables in here, but I didn’t want to flood this with an excessive amount)

My problem is basically in the save/load feature. I’ve asked a similar question before here:
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?

This made me decide it’d be best to use NSCoding, and I’ve already implemented a method for it, too, in accordance with the way that was suggested with NSCoding.

My primary problem is that I can’t get the recipes to be stored and successfully retrieved.

I’ve also had trouble getting the directory to my RecipeList.plist file to store the recipes in.

Any help would be greatly appreciated as this has been the reason I can’t continue making this application.

In my Cookbook.m I have:

-(id) initWithCoder:(NSCoder *)aDecoder{
NSLog(@"Init With Coder - Cookbook");
if(self = [super init]){
    recipes = [aDecoder decodeObjectForKey:@"recipes"];
}
return self;
}

In my Recipe.m:

-(id) initWithCoder:(NSCoder *)aDecoder{
if(self = [super init]){
    name = [aDecoder decodeObjectForKey:@"name"];
    ingredients = [aDecoder decodeObjectForKey:@"ingreds"];
    recipePicture = [aDecoder decodeObjectForKey:@"recipePict"];
}
return self;
}

Once again, I have more variables in there, this is just for simplicity.
Also, this is my attempt at getting a file path to RecipeList.plist:

+(NSString*) filePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
path = [path stringByAppendingPathComponent:@"RecipeList.plist"];

NSLog(@"%@",path);
return path;
}

My attempt at a save method in my AppDelegate.m:

-(void) save:(NSString *)path cookbook:(Cookbook *)cookbook{
BOOL b = [[NSFileManager defaultManager] fileExistsAtPath:path];
NSLog(@"File exists: %i",b); //1 = exists, 0 = doesn't

NSMutableData* data = [[NSMutableData alloc] init];
if(data){ //If the data object was successfully initialized
    NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    if(archiver){
        //Encode the recipe using the coder method defined in recipe.
        [archiver encodeInt:1 forKey:@"Version"];
        [archiver encodeObject:cookbook forKey:@"Cookbook"];
        [archiver finishEncoding];

        [data writeToFile:path atomically:YES];
    }
}
}

and my load method:

-(Cookbook *) loadCookbook:(NSString *)path{
BOOL b = [[NSFileManager defaultManager] fileExistsAtPath:path];
NSLog(@"File exists: %i",b);

Cookbook* ret = nil;
NSData* data = [NSData dataWithContentsOfFile:path];
if(data){
    NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    int version = [unarchiver decodeIntForKey:@"Version"];
    if(version == 1){
        ret = (Cookbook*) [unarchiver decodeObjectForKey:@"Cookbook"];
    }
    [unarchiver finishDecoding];
}
return ret;
}

I also have a save and load method for my Recipe class very similar to this.

Once again, any help would be greatly appreciated, and thank you for taking the time to read through this.

EDIT: Here’s the encodeWithCoder method in Recipe.m with a few variables omitted for the sake of brevity:

-(void) encodeWithCoder:(NSCoder *)aCoder{
NSLog(@"Encoding Recipe.");
[aCoder encodeObject:name forKey:@"name"];
[aCoder encodeObject:ingredients forKey:@"ingreds"];
[aCoder encodeObject:recipePicture forKey:@"recipePict"];
[aCoder encodeObject:instructions forKey:@"instructs"];
[aCoder encodeObject:category forKey:@"categ"];
[aCoder encodeObject:dateAdded forKey:@"dateAdd"];
[aCoder encodeInt:prepTime forKey:@"prepTime"];

}

and in Cookbook.m:

-(void) encodeWithCoder:(NSCoder *)aCoder{
NSLog(@"Encoding cookbook.");
[aCoder encodeObject:recipes forKey:@"recipes"];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:10:10+00:00Added an answer on June 16, 2026 at 9:10 am

    I’ve “almost” copy-pasted your code and after some minor adjustments I’ve got it up and running.

    To the Cookbook, I’ve added:

    - (id)init {
        if (self = [super init]) {
            recipes = [NSMutableArray array];
        }
        return self;
    }
    

    and

    - (void)addRecipe:(Recipe *)recipe {
        [recipes addObject:recipe];
    }
    

    You might have those already? I assume you’re instantiating the array somewhere?

    Then using this test-routine, the cookbook is created, used and saved perfectly fine:

    NSString *path = [self filePath];
    Cookbook *cookbook = [self loadCookbook:path];
    
    if (cookbook) {
        NSLog(@"Loaded cookbook: %@", cookbook);
    }
    else {
        cookbook = [[Cookbook alloc] init];
    }
    
    Recipe *recipe = [[Recipe alloc] init];
    recipe->name = @"The name";
    [cookbook addRecipe:recipe];
    
    [self save:path cookbook:cookbook];
    NSLog(@"Saved cookbook: %@", cookbook);
    

    fyi: I’ve made the Recipe’s instance variables @public for brevity; hopefully you’re using accessors (@property).

    Do you use Automatic or Manual reference counting?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which has BIG numbers and small numbers in it. I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.