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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:33:05+00:00 2026-06-13T17:33:05+00:00

Possible Duplicate: In Objective-C why should I check if self = [super init] is

  • 0

Possible Duplicate:
In Objective-C why should I check if self = [super init] is not nil?

I’m new at Ob-C, and am having a hard time understanding why the value returned is non-nil as tested by the “if statement”.

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

This method invokes the parent initializer first. Executing the parent’s initializer ensures that any inherited instance variables are properly initialized.
You must assign the result of executing the parent’s init method back to self because an initializer has the right to change the location of the object in memory (meaning its reference will change).
If the parent’s initialization succeeds, the value returned will be non-nil, as tested by the if statement. As the comment indicates, inside the block that follows is where you can put your own custom initialization code for your object. This will often involve allocating and initializing instance variables that are in your class.

Pasted code and text from
From Stephen Kochan “Programming in Objective-C, Fourth Edition”

  • 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-13T17:33:06+00:00Added an answer on June 13, 2026 at 5:33 pm

    Consider the following scenario, you have a parent class, which we’ll call Parent, that has the following layout:

    @implementation Parent {
        int value;
    }
    

    and you have a class Child, which is a subclass of Parent, and has the following layout:

    @implementation Child {
        int other;
    }
    

    within the Child subclass, you have no direct access to the Parent ivar called value. However, in its initialization, Parent assigns some integer to value, and some functionality in Parent depends on this value being set.

    if Child does not call [super init], value will never be initialized and part of the functionality of Parent, which Child inherits, will be broken. The init method is defined to return a pointer to the initialized object instance. If you ignore the return value of [super init], you can get into trouble, because the parent init may have decided to reassign the object instance to some location other than the one provided by the allocator.

    For instance, NSString can detect initialization with empty string literals, and will return a pointer to a constant NSString reference instead of one on the heap. So say you just call [super init] and ignore its return value, and keep using the self pointer value passed to the Childs init method, you’re suddenly using a dangling pointer!

    This is an extreme case, but the point is, if you intent to inherit functionality from your parent class, you should assign self to [super init], and you should check if it returns nil because it may have decided to fail initialization and destroy the memory provided to it by the allocator.

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

Sidebar

Related Questions

Possible Duplicate: In Objective-C why should I check if self = [super init] is
Possible Duplicate: Objective-C - When to use 'self' I am having problem understanding the
Possible Duplicate: Why should I call self=[super init] I been reading a book of
Possible Duplicate: alloc, init, and new in Objective-C There is +alloc/-init... and +new .
Possible Duplicate: Should I refer to self.property in the init method with ARC? I'm
Possible Duplicate: Calling Python from Objective-C I'm a long-time Python programmer and short-time Cocoa
Possible Duplicate: String comparison in Objective-C I realize that the question is not very
Possible Duplicate: Understanding NSString comparison in Objective-C I've used this simple code for several
Possible Duplicate: Method overloading in Objective-C? Is method overloading not possible. I have two
Possible Duplicate: Objective c formatting string for boolean? What NSLog % -specifier should be

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.