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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:02:19+00:00 2026-05-27T11:02:19+00:00

I just started to learn iOS programming and I have a problem with inheritance.

  • 0

I just started to learn iOS programming and I have a problem with inheritance. There are 2 files.

First file

Header

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
    int x;
}
@end

Implementation:

#import "ViewController.h"
#import "NewClass.h"
@implementation ViewController
#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    x = 999;
    NewClass *myClass = [[[NewClass alloc] init] autorelease];
}
@end

Second file

Header:

#import "ViewController.h"

@interface NewClass : ViewController 
@end

Implementation:

#import "NewClass.h"
@implementation NewClass

-(id)init { 
    self = [super init];                                       
    if (self != nil) {                                            
        NSLog(@"%i",x);
    }
    return self;                           
}
@end

In ViewController I set x to 999, and in NewClass I want to get it, but when I call NSLog(@"%i",x); it gives me 0.

Where did I make a mistake?

  • 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-27T11:02:20+00:00Added an answer on May 27, 2026 at 11:02 am

    You have a timing problem.

    1. The init method gets called first (at all levels of the inheritance hierarchy, so in both ViewController and NewClass). This is when you print out your value of x, when it is still zero.

    2. The viewDidLoad method only gets called much later, generally at a point after a view controller’s view has been added to a superview. It’s functionality that’s specific to the UIViewController class.

    To make your example work, put an init method in your ViewController class that looks like the one in your NewClass class, and set x there.

    Also, you don’t need to create a NewClass instance within ViewController. When you create a NewClass object, it is automatically a ViewController as well. In the same way that a dog is an animal automatically, and so is a cat. When you create a dog, you don’t want to create an animal as well!

    As sidyll says, you should probably do a bit more reading about how inheritance works, I’m afraid!

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

Sidebar

Related Questions

I just started to learn iOS programming and I have a problem with inheritance.
I just started to learn C programming. In my book there is this piece
I have just started to learn the very basics of Java programming. Using a
I am newbie C# developer. When I just have started to learn programming thins
I've just started to learn about tie . I have a class named Link
I have just started to learn Ruby and got a good take on the
I just have started to learn Haskell and combine reading books and tutorials with
I have just started to learn about the pros of foreign keys in database
I have just started to learn the basics of Blackberry.... So, I am facing
I have just started to learn ASP.NET MVC. When comparing ASP.NET MVC with Web

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.