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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:04:16+00:00 2026-06-15T21:04:16+00:00

New to objective-c. Wrote a code-snippet to better understand the init mechanism, and ended

  • 0

New to objective-c. Wrote a code-snippet to better understand the init mechanism, and ended up with a few questions.

@implementation MyClass    
    -(id) init
    {
        if (self) {
            i = 5;
            NSLog(@"self before init - %@ %p i=%d",[self className], &self, i);
        } else {
            NSLog(@"self is null???");
        }

        id someClass = [super init];
        NSLog(@"the result of super-init - %@ %p",[someClass className], &someClass);

        self = [super init];
        if (self) {
            NSLog(@"self after init - %@ %p %d",[self className], &self, i);
        } else {
            NSLog(@"self is null???");
        }

        return self;
    }

i is a private instance variable int.

Here is the result:

2012-12-14 18:01:26.403 Init[1621:303] self before init - MyClass 0x7fff5fbff848 i=5
2012-12-14 18:01:26.405 Init[1621:303] the result of super-init - MyClass 0x7fff5fbff838
2012-12-14 18:01:26.405 Init[1621:303] self after init - MyClass 0x7fff5fbff848 5

What really surprised me is that that someClass’s class Name is MyClass.

  • How does NSObject know to return the sub-classes instance (not just the type match, it is the exact same object)?

I’m aware that it is not good form to call init many times, and initialize instance variables before calling init but I was just experimenting.

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-06-15T21:04:18+00:00Added an answer on June 15, 2026 at 9:04 pm

    You do need to use the standard schemen (more or less):

    -(id)init {
        self = [super init];
        if (self) {
            // Do initialization stuff
        }
     }
    

    Your class subclasses some other class. The call to super init runs the init routine of your superclass. Without it your class is not properly initialized and may malfunction strangely. (However, it’s probably not wise to call super init twice, as this could have bad side-effects.)

    There are cases where you would not call super init, but would instead call a version of init in your own class. Basically, if you have initWithJunk: and init, you can have initWithJunk: call [self init] instead of [super init] so that the stuff that self init would do gets done and doesn’t have to be reproduced in initWithJunk:.

    This is especially critical if you write a “category” that adds an init... method to an existing class — you must call some version of [self init] to assure that the base class’s initializer runs.

    Understand that the super init method is not (usually) replacing the existing instance with a new one, but rather is initializing instance fields in it that belong to the superclass. The reason for receiving the “self” value back from the super init call is two-fold:

    1. The init routine can return a nil in the event that some sort of error occurs.
    2. In some (rare) special cases the init routine may replace the supplied instance with a different one (eg, a cached version).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Objective-c. I wrote a few lines of code to access a
I'm very new to Objective-c and trying to understand blocks a little better. As
I am quite new to Objective-C and Cocos2d, and i don't really understand why
I'm trying to get a feel for objective C, so I wrote the code
I'm pretty new to objective c, and having some basic problems. I wrote a
New to objective-c. When I write code in strongly-typed OO languages, I like to
I'm new in objective-c, and I'm now trying to write a simple user-sign-in program
I am making a new Objective C class and it gave two options as
I am new to iOS development. I've recently stumbled upon the new objective-c collection-literals
New to objective-C, #import <objc/objc.h> #import <Foundation/Foundation.h> @interface Test:NSObject { int x,y, abc; NSString

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.