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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:06:47+00:00 2026-05-15T05:06:47+00:00

I have a class MyClass. It has instance variables passedInVar1, passedInVar2, etc. whose values

  • 0

I have a class MyClass. It has instance variables passedInVar1, passedInVar2, etc. whose values will be passed in from the object that requests the initialization. It also has instance variables decodedVar1, decodedVar2, etc. that will be decoded from an archive — or set to a default value if there is no archive.

According to Apple,

When an object receives an initWithCoder: message, the object should first send a message to its superclass (if appropriate) to initialize inherited instance variables, and then it should decode and initialize its own instance variables.

But Apple also says that a class should have a single designated initializer.

What is the best way to deal with all of this?

  • 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-15T05:06:48+00:00Added an answer on May 15, 2026 at 5:06 am

    Apples says that:

    designated initializer The init…
    method that has primary responsibility
    for initializing new instances of a
    class. Each class defines or inherits
    its own designated initializer.
    Through messages to self, other
    init… methods in the same class
    directly or indirectly invoke the
    designated initializer, and the
    designated initializer, through a
    message to super, invokes the
    designated initializer of its
    superclass. [emp added]

    In principle, the designated initializer is the one init method that all other init methods call. It is not, however, the only init method. Neither does each class have to have its own. More often in practice the designated initializer is actually the super class’ init.

    The major function of initWithCoder is to allow for initialization from an archived object. In the case of a class which requires some specific data, it’s designated initializer will accept that data. initWithCoder then simply unpacks the archive and then calls the designated initializer.

    For example, the designated initializer for UIView is initWithFrame:. So, UIView’s initWithCoder looks something like:

    - (id)initWithCoder:(NSCoder *)decoder{
        CGRect theFrame= //...uppack frame data
        self=[self initWithFrame:theFrame];
        return self;
    }
    

    The point of the designated initializer is to create a central point that all initialization has to pass through in order to ensure that each instances is completely initialized regardless of where the data came from or the circumstances of the initialization.

    That should never be taken to mean that a class can only have one initializer method.

    Edit01

    From comments:

    In particular, how do I pass values
    for some of my ivars in when
    initialization is happening via
    initWithCoder?

    Well, you don’t. The entire point of initWithCoder is that you are dealing with a freeze dried instance of your class that contains all the data necessary to recreate the object.

    The NSCoding protocol makes your class behave like the brine shrimp they sell as “Sea Monkeys” in the comic books. The coding methods dehydrates/freeze dries the brine-shrimp/instances. The decoding methods hydrates the brine-shrimp/instances just like pouring the brine shrimp into water does. Just like the the brine-shrimp have everything they need to start living except water, a coded object saved on disk has all the data needed to recreate itself once initialized with the coder.

    The canonical example of this is a nib file. A nib file is just a bunch of freeze dried instances of UI elements and controllers. A UIViewController and its UIViews in a nib have all the data they need to initialize themselves coded into the xml of the nib file. When you call initFromNib directly or with an IBOutlet, it calls each class’ intiWithCoder: method.

    If you don’t save the complete object when you freeze dry it, then the attributes that don’t get freeze dried aren’t needed for the instance object to exist.

    You just set those ancillary attributes after the object has been initialized.

    To inline the designated initializer, you just decode first and then call the designated initializer. Like so:

    -(id) initWithRequiredValue:(id) someValue otherRequiredValue:(id) anotherValue{
        if (self=[super init]){
            self.requiredProperty=someValue;
            self.anotherRequiredProperty=anotherValue
        }
        return self;
    }
    

    If the super class does not support NSCoder then you start it yourself in the subclass:

    - (id)initWithCoder:(NSCoder *)decoder {
        id someDecodedValue=[decoder decodeObjectForKey:@"someValueKey"];
        id someOtherDecodedValue=[decoder decodeObjectForKey:@"someOtherValueKey"];
        self=[self initWithRequiredValue:someDecodedValue otherRequiredValue:someOtherDecodedValue];
        return self;
    }
    

    That’s the simplest case. If super itself supports NSCoding, then you usually just end up writing a parallel designated initializer like so:

    - (id)initWithCoder:(NSCoder *)decoder {
        if (self=[super initWithCoder:decoder]){
            id someDecodedValue=[decoder decodeObjectForKey:@"someValueKey"];
            id someOtherDecodedValue=[decoder decodeObjectForKey:@"someOtherValueKey"];
            self.requiredProperty=someDecodedValue;
            self.anotherRequiredProperty=someOtherDecodedValue;
        }
        return self;
    }
    

    I think in most cases, initWithCoder ends up being a parallel designated initializer because it takes care of all initialization just like the designated initializer should. It doesn’t look like the designated initializer because all its data is provided by the coder but it performs the same function.

    This is one of those cases where theory and practice don’t line up well. The “designated initializer” concept really only applies to cases wherein you create instances from scratch.

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

Sidebar

Related Questions

I have a Singleton Class SharedDataObject which has a another class object myClass. MyClass
I have a class: class MyClass(object): @property def myproperty(self): return 'hello' Using mox and
Suppose I have this class: class MyClass(object): def uiFunc(self, MainWindow): self.attr1 = foo self.attr2
I have a serialized class that has variables ABC. I declare a serialVersionUID in
I have a class MyClass , which contains two member variables foo and bar
Say I have an object called MyClass, which has a property defined as @property
I have a class MyClass , which contains two member variables foo and bar
Fairly simple question: I have an init method on my class that has the
Heyo, My class at college has us writing programs in assembly. I have never
I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private

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.