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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:39:59+00:00 2026-06-17T22:39:59+00:00

I’m feeling very stupid, but…what is the right way to init instance variables in

  • 0

I’m feeling very stupid, but…what is the right way to init instance variables in custom types, derived from Cocoa Touch UI classes?

Say I have type, derived from UIViewController, let it be TRUIController.
I defined an ivar as follows:

@implementation TRUIController
{
    NSNumberFormatter *_numberFormatter;
}
@end

And where should I put _numberFormatter initialization code, if I want it to be executed before any UI methods like viewDidLoad, etc?

In other languages I would create constructor, call base constructor and then init my ivars.
But that simply does not work in objective-c and Cocoa Touch.

In case described above,
If I write

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibBundleOrNil bundle:nibBundleOrNil];
    if(self)
    {
        //init ivars
    }
    return self;
}

It won’t work, I’ll try other initWithBlaBla methods, and finally find init, which is actually called, in that case – initWithCoder:

Now, let’s say I have class, derived from UITableViewCell. Again, need to init NSNumberFormatter ivar. Why can’t I just override init:, call super and init my ivars?

What’s the idea behind this strange design decision not to have single common initialization method for all types?

Is it the only way to initialize ivars in derived types? Every time do some research to figure out what method to override this time?

Have I missed something? Because it feels extremely stupid/unintuitive/error-prone

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

    You have to do so overriding the designated initializer.

    From the official documentation

    A designated initializer is an init method of a class that invokes an
    init method of the superclass. (Other initializers invoke the init
    methods defined by the class.) Every public class should have one or
    more designated initializers. As examples of designated initializers
    there is NSView’s initWithFrame: and NSResponder’s init method. Where
    init methods are not meant to be overridden, as is the case with
    NSString and other abstract classes fronting class clusters, the
    subclass is expected to implement its own.

    Designated initializers should be clearly identified because this
    information is important to those who want to subclass your class. A
    subclass can just override the designated initializer and all other
    initializers will work as designed.

    When you implement a class of a framework, you often have to implement
    its archiving methods as well: initWithCoder: and encodeWithCoder:. Be
    careful not to do things in the initialization code path that doesn’t
    happen when the object is unarchived. A good way to achieve this is to
    call a common routine from your designated initializers and
    initWithCoder: (which is a designated initializer itself) if your
    class implements archiving.

    Read the documentation for finding out which initializer is the designated one and override it.

    According to the documentation of UIViewController initWithNibName:bundle: is the designated initializer for this class is so you should override that one, but if you are not programmatically instantiating your class (e.g. you’re loading it from nib/storyboard) you have to override the archiving method initWithCoder:.

    As suggested by the documentation you can create a routine for initialization and call it from both the designated initializer and the archiving method.

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

Sidebar

Related Questions

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,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string

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.