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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:32:43+00:00 2026-05-26T12:32:43+00:00

I have a class named Defense with custom init method as below: // initialize

  • 0

I have a class named “Defense” with custom init method as below:

// initialize the defense unit and add the sprite in the given layer
- (id) initWithType:(DefenseType)tType andInLayer:(CCLayer *)layer {

    NSString       *fileName;
    fileName     = [NSString stringWithUTF8String:defenseStructuresFile[tType]];

    if ( (self = [super initWithFile:fileName]) ) {

        type              =   tType;
        maxHealth         =   defenseStructuresHealth[tType];
        health            =   maxHealth;
        mapRef            =   (SkirmishMap *)layer;        
    }
    return self;
}

now i am making my class NSCoding compatible, which requires the following 2 methods:

- (id) initWithCoder:(NSCoder *)decoder
- (void) encodeWithCoder:(NSCoder *)encoder

When I normally allocate an instance of “Defense”, I code as follows:

Defense                 *twr;
twr                 =   [[Defense alloc] initWithType:type andInLayer:mapRef];

and when I want to restore a saved instance of Defense object I code as

twr                 =   [[decoder decodeObjectForKey:kDefense] retain];

But in the above code I cant pass the “type” and “mapref” parameters, which are very much required to initialize the object…

The Defense class derives from CCSprite and since CCSprite doesn’t conform to NSCoding, it is fine to call (self = [super initWithFile:fileName]) from my initWithCoder method. But I require the type parameter to determine the filename to be passed to ccsprite's initWithFile.

So what would I do in this situation?
Should I change my class design? if yes, how?

Any good ideas/suggestions are highly 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-05-26T12:32:44+00:00Added an answer on May 26, 2026 at 12:32 pm

    You can do something like this:

    - (id)initWithCoder:(NSCoder *)decoder
    {
        DefenseType earlyType;
        NSString *filename;
    
        earlyType = [decoder decodeIntegerForKey:@"defense"];
    
        if (earlyType == somethingIllegal) {
            [self release];
            return nil;
        }
    
        // Now do something with earlyType do determine filename
    
        self = [super initWithFilename:filename];
        if (!self) return nil;
    
        type = earlyType;
        // Decode the rest.
    
        return self;
    }
    

    When your methods are entered (even init‘s) then self is always set. And it’s just a variable (or to be more precise: an argument). This is why [self release]; return nil; will work even before calling super. When you call self = [super initWithFoo:]; you’re overwriting self. This is done because your super class might do [self release]; return nil; or allocate a concrete subclass and return that instead (when using a class cluster). So before you call super’s initializer, it’s not safe to overwrite instance variables, but it’s OK to use normal stack variables (or even global variables).

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

Sidebar

Related Questions

I have a class named insect child of sprite.I have created a instance of
i have class named Group i tried to test configuration: var cfg = new
I have a class named Page with a private property currently called _pageData which
I have a class named baseClass. From this class I inherit a class names
I have a Class named Constants that contains all the constant variable in my
I have a class named Person and in this class is the property PersonName
I have a class named project with the following data members. class Project {
I have a class named toto which I send to a function that does
I have a class named SuperclassA , and an class named ClassA . ClassA
Say I have a class named Frog, it looks like: public class Frog {

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.