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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:39:58+00:00 2026-05-19T05:39:58+00:00

The Cocoa framework has a convention to always call self = [super init] in

  • 0

The Cocoa framework has a convention to always call self = [super init] in the init method of an inherited class, because [super init] may return a new instance.

What will happen if I do this?

@interface MyClass : NSObject /* or any other class */ {
    int ivar_;
}
@end

@implementation MyClass

- (id)init {
    ivar_ = 12345;

    if ((self = [super init])) {
        NSLog(@"ivar_'s value is %d", ivar_);
    }
    return self;
}

@end

In the case when [super init] returns a new instance, what will I see in the console? ivar_'s value is 0?

I can’t think of a way to check this myself, because I don’t know which class may return a new instance from its init method. Also, can’t seem to find explicit clarification for this scenario in the docs.

Could anyone help me out? Thanks!

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

    Under normal circumstances, when you send +alloc to a class it returns a zeroed out instance of that class. It is already initialised in the sense that: a) it is a proper instance of that class, and b) all instance variables are zeroed out (numeric types are 0, C pointers are NULL, objects are nil, etc). However, no extra initialisation behaviour has been applied to the newly created instance — that’s the job of initialisers, -init being the most common of them. In particular, if your class inherits from NSObject then [super init] doesn’t touch the newly created instance. So, in the code you’ve posted in your question,

    1. (outside the class) +alloc is sent to MyClass, which returns a zeroed out instance. In particular, ivar_ is 0
    2. (outside the class) -init is sent to the newly created instance
    3. ivar_ == 12345 because of the assignment in -init, which could be rewritten as self->ivar_ = 12345. At this moment, self points to the instance returned by +alloc
    4. [super init] is sent, ivar_ isn’t touched since its superclass (NSObject) doesn’t know about it, and the return value is assigned to self. In fact, the return value is already self so nothing’s changed
    5. Since self is different from nil, NSLog() is called
    6. -init returns self

    Now let’s consider that [super init] returns an instance that’s different from the one returned by +alloc:

    1. (outside the class) +alloc is sent to MyClass, which returns a zeroed out instance. In particular, ivar_ is 0
    2. (outside the class) -init is sent to the newly created instance
    3. ivar_ == 12345 because of the assignment in -init, which could be rewritten as self->ivar_ = 12345. At this moment, self points to the instance returned by +alloc
    4. [super init] is sent and it decides to release the current instance and return a different one. The return value is assigned to self so, from the current perspective, -init is now working on a different instance. The old instance was deallocated, hence that change to ivar_ is lost. ivar_ contains whatever [super init] decides it should contain
    5. Since self is different from nil, NSLog() is called
    6. -init returns self, which is not the same instance created by +alloc

    You can test this with the following code. Be warned that it’s just a quick example to illustrate my answer and it should not be used in production code.

    #import <Foundation/Foundation.h>
    
    @interface MySuperClass : NSObject
    @end
    
    @interface MyClass : MySuperClass
    {
      @public
      int ivar_;
    }
    @end
    
    @implementation MySuperClass
    
    static MyClass *defaultInstance;
    
    - (id)init
    {
      if ([self isMemberOfClass:[MyClass class]] && defaultInstance != nil)
      {
        [self release];
        return defaultInstance;
      }
    
      return [super init];
    }
    @end
    
    @implementation MyClass
    - (id)init
    {
      ivar_ = 12345;
    
      if ((self = [super init]))
        NSLog(@"ivar_'s value is %d", ivar_);
    
      return self;
    }
    @end
    
    int main()
    {
      NSAutoreleasePool *pool = [NSAutoreleasePool new];
    
      defaultInstance = nil;
      defaultInstance = [[MyClass alloc] init];
      NSLog(@"%d", defaultInstance->ivar_); // outputs 12345
    
      defaultInstance->ivar_ = 98765;
      NSLog(@"%d", defaultInstance->ivar_); // outputs 98765
    
      MyClass *someInstance = [[MyClass alloc] init];
      NSLog(@"%d", someInstance->ivar_); // outputs 98765
    
      if (someInstance == defaultInstance)
        NSLog(@"They're the same!");
    
      [pool drain];
      return 0;
    }
    

    The output is

    ivar_'s value is 12345
    12345
    98765
    ivar_'s value is 98765
    98765
    They're the same!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found an example of Objective-C/cocoa framework has the following code. int main() {
With the Cocoa framework how can I parse @2008-12-29T00:27:42-08:00 into an NSDate object? The
Using the Apple OS X Cocoa framework, how can I post a sheet (slide-down
I'm trying to convert old QuickTime framework code to the 64-bit Cocoa-based QTKit on
In Cocoa, NSTextView has a rather shallow margin between text and border. Is there
I have been developing with objective C and the Cocoa framework for quite some
I find it hard to find some working examples of Cocoa/Carbon framework functions, whereas
I'm in urgent need of an Objective-C/Cocoa/Cocoa Touch framework to handle simple charting; pie
I'm writing a Django-based webapp that imports a Cocoa framework via PyObjC. The Cocoa
I've got a Hybrid Cocoa/Webkit application that has extensive Javascript usage, and seems to

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.