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 7640667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:43:40+00:00 2026-05-31T08:43:40+00:00

I have an array of objects that I want to save as a file

  • 0

I have an array of objects that I want to save as a file and reload back into my app. It’s saving the file (with some data inside) but I can’t get it to read back into a NSMutable Array.

The objects are models that conform to the NSCoding protocol:

@implementation myModel

@synthesize name;
@synthesize number;

-(void) encodeWithCoder: (NSCoder *) encoder
{
    [encoder encodeObject:name forKey:@"name"];
    [encoder encodeInteger:number forKey:@"number"];
}

-(id) initWithCoder: (NSCoder *) decoder
{
    name = [decoder decodeObjectForKey:@"name"];
    number = [decoder decodeIntegerForKey:@"number"];
    return self;
}

@end

So I create an array of these objects, then I save it…

- (void) saveMyOptions {

    // Figure out where we're going to save the app's data files
    NSString *directoryPath = [NSString stringWithFormat:@"%@/Library/Application Support/MyAppDir/", NSHomeDirectory()]; // points to application data folder for user

    // Figure out if that directory exists or not
    BOOL isDir;  

    NSFileManager *fileManager = [[NSFileManager alloc] init];

    [fileManager fileExistsAtPath:directoryPath isDirectory:&isDir];

    // If the directory doesn't exist, create it
    if (!isDir)
    {
        [fileManager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:NULL];
    }

    // Assemble everything into an array of objects with options
    NSMutableArray *savedPreferences = [[NSMutableArray alloc] init];

    myModel *saveOptions = nil;

    for (int i; i < [otherArray count]; i++)
    {
        saveOptions = [[myModel alloc] init];

        [saveOptions setName:@"Some String"];
        [saveOptions setNumber:i];        
        [savedPreferences addObject:saveOptions];
        saveOptions = nil;
    }

    // Actually save those options into a file
    NSData* saveData = [NSKeyedArchiver archivedDataWithRootObject:savedPreferences];

    NSString *fileName = [NSString stringWithFormat:@"%@filename.stuff", directoryPath];

    NSError *error = nil;

    BOOL written = [saveData writeToFile:fileName options:0 error:&error];

    if (!written)
    {
        NSLog(@"Error writing file: %@", [error localizedDescription]);
    }

}

So now I try to load that data back into an array. This is where I think it’s falling apart…

- (NSMutableArray *) loadOptions {

    // Create file manager object
    NSFileManager *fileManager = [[NSFileManager alloc] init];

    NSData *saveData = nil;

    // Find user directory path
    NSString *directoryPath = [NSString stringWithFormat:@"%@/Library/Application Support/MyAppDir/", NSHomeDirectory()]; // points to application data folder for user

    // Assign file name
    NSString *fileName = [NSString stringWithFormat:@"%@filename.stuff", directoryPath];

    // Create options array
    NSMutableArray *myOptions = nil;

    // If the file exists, fill the array with options
    if ([fileManager fileExistsAtPath:fileName])
    {
        saveData = [NSData dataWithContentsOfFile:fileName];
        myOptions = [NSKeyedUnarchiver unarchiveObjectWithData:saveData];
    } 


    NSLog(@"%lu", [myOptions count]); // This ALWAYS reports 0!
    NSLog(@"%lu", [saveData length]); // This reports a value of 236;

    return myOptions;
}

Could someone point me in the direction of where I’m going wrong? I’m throughly confused 🙁

Thanks in advance!

  • 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-31T08:43:41+00:00Added an answer on May 31, 2026 at 8:43 am

    You are missing the super calls in your encodeWithCoder: and initWithCoder: methods, but that’s just a guess. Why not use NSUserDefaults for saving preferences?

    You might also want to make sure that your objects are retained is set using the synthesized setter.

    - (void)encodeWithCoder:(NSCoder *)encoder {
      [super encodeWithCoder:encoder];
      [encoder encodeObject:name forKey:@"name"];
      [encoder encodeInteger:number forKey:@"number"];
    }
    
    - (id)initWithCoder:(NSCoder *)decoder {
      self = [super initWithCoder:decoder];
      if (self) {
        self.name = [decoder decodeObjectForKey:@"name"];
        self.number = [decoder decodeIntegerForKey:@"number"];  
      }
      return self;
    }
    

    For your info, NSKeyedArchiver also has a method you can use directly to operate on files:

    + (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path
    

    and NSKeyedUnarchiver:

    + (id)unarchiveObjectWithFile:(NSString *)path
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mutable array that is retained and storing several objects. At some
I have applications(WinForm) that gets some objects from webservice. After receiving array I transform
I have an array of different objects with different data types I want to
I have an Array of Objects that need the duplicates removed/filtered. I was going
I have an array of objects that have QTTime structs as attributes. The objects
I have a vector-like class that contains an array of objects of type T
In a Flex project, I have an array with objects in it. I want
In my app I have 5 String arrays that represent different fields of objects.
I have a winform application that uses some referenced web services to get data.
I'm saving NumPy arrays using numpy.save function. I want other developers to have capability

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.