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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:23:38+00:00 2026-05-15T04:23:38+00:00

Let’s say I create my class and its init method. Why should I call

  • 0

Let’s say I create my class and its init method. Why should I call and return value of superclass init assigned to self? Which cases it covers?

I would appreciate examples why would I need it for Cocoa superclass and non-Cocoa.

  • 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-15T04:23:39+00:00Added an answer on May 15, 2026 at 4:23 am

    You mean why

    self = [super init];
    

    rather than

    [super init];
    

    Two reasons:

    1. in initialisation, failure is indicated by returning nil. You need to know if initialisation of the super object failed.
    2. the super class might choose to replace the self returned by +alloc with a different object. This is rare but happens most frequently with class clusters.

    Edited in response to Michael’s comment:

    I can understand why I need to save and return [super init]. But is it just convention and good looking makes us use self as a temporary variable to pass result along?

    No. Instance variables are accessed relative to the self pointer, so in the following:

    -(id) init
    {
        self = [super init];
        if (self != nil)
        {
            myBoolIvar = YES; 
           // The above is an implicit version of self->myBoolIvar = YES;
        }
        return self;
    }
    

    self has clearly got to point to the right block of memory i.e. the one you are going to return.

    The other point is that if super init returns different class instance then the rest of the code after that line may not even make sense, lead to memory leaks and crashes, not even talking about the object instantiated from that class.

    That could be a problem. If I subclassed NSNumber and [super init] decided to return an NSString (which it could – there’s nothing to stop it) that would clearly be a disaster. Whatever super returns from -init must be “compatible” with the subclass in the sense of providing space for ivars and being further subclassible or it’s a horrendous bug (unless, of course, the problem is documented). So, in general, you don’t need to worry about checking the class. However, do read the documentation. See for instance the section on subclassing NSString in NSString’s docs.

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

Sidebar

Ask A Question

Stats

  • Questions 428k
  • Answers 429k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I used: if (NSClassFromString(@"MPMoviePlayerViewController") != nil && [UIViewController instancesRespondToSelector: @selector(presentMoviePlayerViewControllerAnimated:)])… May 15, 2026 at 1:26 pm
  • Editorial Team
    Editorial Team added an answer If you like to use a void signature, use a… May 15, 2026 at 1:26 pm
  • Editorial Team
    Editorial Team added an answer The QDataWidgetMapper class is exactly what you want, to edit… May 15, 2026 at 1:26 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.