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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:28:17+00:00 2026-05-14T02:28:17+00:00

I am trying to learn how NSCoding works, but seem to have come accross

  • 0

I am trying to learn how NSCoding works, but seem to have come accross an issue when I try and unachive a previously archived object, this is what I am doing …

// CREATE EARTH & ADD MOON
Planet *newPlanet_002 = [[Planet alloc] init];
[newPlanet_002 setName:@"Earth"];
[newPlanet_002 setType:@"Terrestrial Planet"];
[newPlanet_002 setMass:[NSNumber numberWithInt:1]];
[newPlanet_002 addMoon:[moonsOfEarth objectAtIndex:0]];

// ARCHIVE PLANET OBJECTS (Save)
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:newPlanet_002 forKey:@"EARTH"];
[archiver finishEncoding];

BOOL success = [data writeToFile:PathName atomically:YES];
if(success == YES) NSLog(@"Write: OK");
[archiver release];
[data release];

// UNARCHIVE PLANET EARTH (Load)
NSData *dataStore = [[NSData alloc] initWithContentsOfFile:PathName];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:dataStore];
[unarchiver finishDecoding];

Planet *savedPlanet = [unarchiver decodeObjectForKey:@"EARTH"];

The problem seems to be the last line above, which I guess I am getting wrong. My class looks like …

@interface Planet : NSObject <NSCoding>
{
    NSString *name;
    NSString *type;
    NSNumber *mass;
    NSMutableArray *moons;
}
@property(retain, nonatomic) NSString *name;
@property(retain, nonatomic) NSString *type;
@property(retain, nonatomic) NSNumber *mass;
@property(retain, nonatomic) NSMutableArray *moons;

and my initWithCoder like this …

-(id)initWithCoder:(NSCoder *)aDecoder {
    NSLog(@"_initWithCoder:");
    self = [super init];
    if(self) {
        [self setName:[aDecoder decodeObjectForKey:@"NAME"]];
        [self setType:[aDecoder decodeObjectForKey:@"TYPE"]];
        [self setMass:[aDecoder decodeObjectForKey:@"MASS"]];
        [self setMoons:[aDecoder decodeObjectForKey:@"MOONS"]];
    }
    return self;
}

any help would be much appreciated.

gary

  • 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-14T02:28:17+00:00Added an answer on May 14, 2026 at 2:28 am

    Once you’ve called finishDecoding, you can’t then call decodeObjectForKey:. You obviously haven’t “finished decoding” yet. You call finishDecoding when you’re done.

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

Sidebar

Related Questions

trying to learn and practice arrays but I have a problem with this small
I am somewhat new to jQuery but have been having fun trying learn about
Trying to learn Sencha Touch 2 and I can't seem to find out how
Trying to learn about php's arrays today. I have a set of arrays like
Im trying to learn a bit about c++ and have run in to some
Trying to learn F# but got confused when trying to distinguish between fold and
im trying to learn how to use javascript objects, and got some issue here.
Trying to learn Regex in Python to find words that have consecutive vowel-consonant or
In trying to learn how to create objects in ActionScript, I have had no
Im trying to learn Sync framework. I have followed step by step MSDN Documentation

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.