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

  • Home
  • SEARCH
  • 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 4080756
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:58:15+00:00 2026-05-20T17:58:15+00:00

I’ve used this tutorial to create an app with a table view that is

  • 0

I’ve used this tutorial to create an app with a table view that is populated using an NSMutableArray. Now I’d like to add the functionality to add additional items to the array and save/load them. I’ve customized the Fruit class to look like this:

#import <UIKit/UIKit.h>

@interface Fruit : NSObject {
NSString *name;
NSString *instructions;
NSString *explination;
NSString *imagePath;
}

@property(nonatomic,copy) NSString *name;
@property(nonatomic,copy) NSString *instructions;
@property(nonatomic,copy) NSString *explination;
@property(nonatomic,copy) NSString *imagePath;

- (id)initWithName:(NSString*)n instructions:(NSString *)inst explination:(NSString *)why imagePath:(NSString *)img;

@end

and the Fruit.m file:

#import "Fruit.h"

@implementation Fruit
@synthesize name,instructions,explination,imagePath;

- (id)initWithName: (NSString*)n instructions:(NSString*)inst explination:(NSString *)why imagePath:(NSString *)img {
    self.name = n;
    self.instructions = inst;
    self.explination = why;
    self.imagePath = img;
    return self;
}
@end

and this works great, I can load two textviews and an imageView, instead of just one textview. But how would I go about saving any new items the user creates, and loading them (if they exist) when the app gets launched again?

  • 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-05-20T17:58:15+00:00Added an answer on May 20, 2026 at 5:58 pm

    to save your array to disk you need a couple of things.

    first you need to add some methods to your fruit class so it conforms to the NSCoding protocol.

    The first method is - (id)initWithCoder:(NSCoder *)aDecoder. This method will be called when you create a Fruit object from a saved archive.
    Second method is - (void)encodeWithCoder:(NSCoder *)aCoder. This method is used to save your fruit in an archive.

    Sounds complicated? Actually it isn’t. Just a couple lines of easy to understand code.

    - (id)initWithCoder:(NSCoder *)aDecoder {
        self = [super init];
        if (self) {
            self.name = [aDecoder decodeObjectForKey:@"name"];
            self.instructions = [aDecoder decodeObjectForKey:@"instructions"];
            self.explanation = [aDecoder decodeObjectForKey:@"explanation"];
            self.imagePath = [aDecoder decodeObjectForKey:@"imagePath"];
        }
        return self;
    }
    

    Look at first two lines of this init method. You have to call [super init] and do a check if self is not nil in your initWithName:instructions:explination:imagePath: method too. It won’t change anything in this special case, but this will definitely change in the next few classes you write. So use it all the time.
    I changed this for you. And I changed the spelling error.

    - (id)initWithName: (NSString*)n instructions:(NSString*)inst explination:(NSString *)why imagePath:(NSString *)img {
        self = [super init];
        if (self) {
            self.name = n;
            self.instructions = inst;
            self.explanation = why;
            self.imagePath = img;
        }
        return self;
    }
    

    and the method for encoding:

    - (void)encodeWithCoder:(NSCoder *)aCoder {
        [aCoder encodeObject:name forKey:@"name"];
        [aCoder encodeObject:instructions forKey:@"instructions"];
        [aCoder encodeObject:explanation forKey:@"explanation"];
        [aCoder encodeObject:imagePath forKey:@"imagePath"];
    }
    

    It’s not necessary that the key name matches the variable name. You don’t need to do this. But in my opinion it adds some clarity. As long as you decode with the same name you’ve used for encoding you can use whatever you want.


    First part is done. Next you need to load and save your NSMutableArray to a file. But to do this you need the path to the documents directory. So I created a little helper method that goes into your controller.

    - (NSString *)applicationDocumentsPath {
        return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    }
    

    Then we need to load the array from disk.

    NSString *path = [[self applicationDocumentsPath] stringByAppendingPathComponent:@"some.fruits"];
    
    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    if (!array) {
        // if it couldn't be loaded from disk create a new one
        array = [NSMutableArray array];
    }
    

    then you add as much fruits as you like, and finally, to save your array to disk you need this line.

    BOOL result = [NSKeyedArchiver archiveRootObject:array toFile:path];
    

    you can check result if the archive was done without error.


    I guess this should get you started. Happy coding.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post
I am using Paperclip to handle profile photo uploads in my app. They upload

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.