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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:56:15+00:00 2026-06-02T22:56:15+00:00

I’m hoping this isn’t something to do with the fact that I’m using a

  • 0

I’m hoping this isn’t something to do with the fact that I’m using a Mutable array here, but this one is baffling me so it wouldn’t surprise me if that were the case.

BACKGROUND:

I have made a small database which is essentially an NSMutableArray containing custom objects, which we can call recordObjects. I set up the array:

database = [[NSMutableArray alloc] init];

and my custom object, called “recordObject” contains the following variables and inits:

NSString *name;
int       anInt;
Bool      aBool;

I also synthesized methods so I can make calls like:

aString = [[database objectAtIndex:someIndex] name];

And added methods to my controller class to add, remove, and select the individual records for display. So far everything works correctly and exactly as expected.

Next, I’ve set up my recordObject class (subclass of NSObject) to use the NSCoder (by including in the @interface directive, and have added the following custom encoder and decoder methods in the implementation file:

-(void) encodeWithCoder: (NSCoder *) encoder {
    [encoder encodeObject: name  forKey: @"recordName"];
    [encoder encodeInt:    anInt forKey: @"recordInteger"];
    [encoder encodeBool:   aBool forKey: @"recordBool"];
}

-(id) initWithCoder: (NSCoder *) decoder {
    name    = [decoder decodeObjectForKey: @"recordName"];
    anInt   = [decoder decodeIntForKey:    @"recordInteger"];
    aBool   = [decoder decodeBoolForKey:   @"recordBool"];
}

In order to write the file, I have used the following:

[NSKeyedArchiver archiveRootObject:database toFile:myPath];

When I run the program, everything APPEARS to work correctly. The encoder method is called for each of the records in the array and the file is written to disk. Opening the file with TextEdit shows that the data is there (though mostly unintelligible to me.)

THE PROBLEM:

Here’s where I run into a snag.

I added the following code to LOAD the file into my database array:

database = [NSKeyedUnarchiver unarchiveObjectWithFile:myPath];

When I run the program again, this time Loading the database, it APPEARS to work correctly. My first test was to use the NSMutableArray count method:

x = [database count];

The result was that X is filled with the correct number of records in the file. If there were 5 records when I saved the database, X is set to 5 after loading the database on the next execution of the program.

Now, here’s the big problem:

The program crashes if I try to use ANY of my accessor methods. For example, if I try to use the following after loading the database:

aString = [[database objectAtIndex:someIndex] name];

the program crashes and returns the following error in the console:

Program received signal:  “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all

My interpretation is that the data is not being loaded and initialized into the database array correctly for some reason, but for the life of me I can’t figure out where I’ve gone wrong here.

As a side note, everything I’ve implemented came from Stephen G. Kochan’s book “Programming in Objective-C”

Any ideas would be greatly appreciated!

  • 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-02T22:56:17+00:00Added an answer on June 2, 2026 at 10:56 pm

    There are a few problems with your code.

    Your initWithCoder: method is not fully implemented. You must call [super init] and return self. You must also copy or retain the string object, otherwise it will be autoreleased:

    - (id)initWithCoder:(NSCoder *)decoder 
    {
        self = [super init];
        if(self)
        {
             name    = [[decoder decodeObjectForKey: @"recordName"] copy];
             anInt   = [decoder decodeIntForKey:    @"recordInteger"];
             aBool   = [decoder decodeBoolForKey:   @"recordBool"];
        }
        return self;
    }
    

    The other problem is with this line:

    database = [NSKeyedUnarchiver unarchiveObjectWithFile:myPath];
    

    That is fine, except for two things:

    1. You’re not holding a reference to the object, so it will be
      autoreleased.
    2. NSKeyedArchiver returns an immutable object, in this case an NSArray and not an NSMutableArray.

    You need to do this:

    database = [[NSKeyedUnarchiver unarchiveObjectWithFile:myPath] mutableCopy];
    

    That will both retain the object (because it’s a copy) and make the object an NSMutableArray.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
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,

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.