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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:29:51+00:00 2026-05-27T13:29:51+00:00

I have just started studying Objective – C..and I encountered a problem which I

  • 0

I have just started studying Objective – C..and I encountered a problem which I have been trying to solve for hours now..Here’s my code..

ClassA.h

#import <Foundation/Foundation.h>

// The ClassA Class

// @interface section
@interface ClassA: NSObject
{
    NSMutableArray *x;
}
@property (copy,nonatomic) NSMutableArray *x;
- (void) initVar;
- (void) print;
- (void) addStr: (NSString *) str;
@end

ClassA.m

#import "ClassA.h"

// @implementation section
@implementation ClassA

@synthesize x;
- (void) initVar
{
    x = [[NSMutableArray alloc] init];
}

- (void) print
{ 
    for(NSString *str in x)
        NSLog (@" --------- %@", str);

}

- (void) addStr: (NSString *) str
{
    if(![x containsObject: str])
        [x addObject: str];
}
@end

ClassB.h

#import "ClassA.h"

// The ClassB Class

// @interface section
@interface ClassB: ClassA
{
    NSMutableArray *y;
}
- (void) initVar;
- (void) print;
- (void) addStr: (NSString *) str;
@end

ClassB.m

#import "ClassB.h"

// @implementation section
@implementation ClassB

- (void) initVar
{
    y = [[NSMutableArray alloc] init];
}

- (void) print
{ 
    for(NSString *str in y)
        NSLog (@" />>>>>>>>> %@", str);
}

- (void) addStr: (NSString *) str
{
    if(![self.x containsObject: str] && ![y containsObject: str])
    {
        [self.x addObject: str];
        [y addObject: str];
    }
    else if([self.x containsObject: str] && ![y containsObject: str])
    {
        [y addObject: str];
    }
}

@end

Here’s where I’m calling the classes

#import "./MyClasses/ClassB.h"

int main(int argc, const char * argv[])
{
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSLog (@"This will print!");
    ClassB *b = [[ClassB alloc] init];
    ClassA *a = [[ClassA alloc] init];

    [a initVar];
    [a addStr: @"obj1"];
    [a addStr: @"obj2"];
    [a addStr: @"obj3"];
    [a print];

    [b initVar];
    [b addStr: @"YOH1"];
    [b addStr: @"YOH2"];
    [b addStr: @"YOH3"];
    [b print];

    NSLog (@" +++++++++++++++++++++++++++++++++ ");

    [a print];

    [a release];
    [b release];
        [pool drain];
        return 0;
}

I want it’s output is like this..

This will print!
———— obj1

———— obj2

———— obj3

/>>>>>>>>>>>> YOH1

/>>>>>>>>>>>> YOH2

/>>>>>>>>>>>> YOH3

++++++++++++++++++++++++++++++++++++

———— obj1

———— obj2

———— obj3

———— YOH1

———— YOH2

———— YOH3


Currenty the last 3 output ( YOH1, YOH2 and YOH3) aren’t showing. Meaning that I didn’t add the object to xsuccessfully. What am I doing wrong?

  • 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-27T13:29:51+00:00Added an answer on May 27, 2026 at 1:29 pm

    So the ‘b’ object doesn’t even have an NSMutableArray in ‘x’. ‘x’ will be nil for the object ‘b’.

    But I suspect there’s a different misunderstanding here. The object ‘b’ and the object ‘a’ are two different objects. Just because class B is a subclass of A, doesn’t mean that adding strings to some instance of class B is going to add those strings to some other instance of class A.

    To answer your question in the comments, suppose you did want every instance of class B to have its own NSMutableArray in ‘x’. You can do this like so:

    // in ClassA.h
    @interface ClassA: NSObject {
       NSMutableArray *x;
    }
    
    @property (retain,nonatomic) NSMutableArray *x;
    @end
    
    // in ClassA.m
    
    @implementation ClassA
    @synthesize x;
    
    - (id)init {
        if ((self = [super init])) {
    
          x = [[NSMutableArray alloc] init];
          // whenever we init an instance of class A, we will have
          // an nsmutablearray in 'x'
        }
        return self;
    }
    
    - (void)dealloc {
    
       // assume you aren't using ARC
       [x release];
       [super dealloc];
     }
    
    // in ClassB.h
    @interface ClassB: ClassA {
       NSMutableArray *y;
    }
    
    @property (retain,nonatomic) NSMutableArray *y; // for consistency
    @end
    
    // in ClassB.m
    
    @implementation ClassB
    @synthesize y;
    
    - (id)init {
        if ((self = [super init])) { // call Class A's init
    
          y = [[NSMutableArray alloc] init];
          // whenever we init an instance of class B, we will have
          // everything an instance of A has, plus this y we need
        }
        return self;
    }
    
    - (void)dealloc {
    
        // assume you aren't using ARC
        [y release];
        [super dealloc];
    }
    

    I moved your initVar into a proper init, there’s no reason to have two methods there. And I’m just showing the init’s only, leaving out the addString and print.

    So now you can do this:

    ClassB *instanceOfB = [[ClassB alloc] init];

    and that instance will have both the x and y arrays that you want.

    I left the instance variable declarations in there also, even though they aren’t strictly required with the newer compilers. The newer compiler versions can implicitly define those instance variables. If you don’t understand that, don’t worry about it at this point. Not that important.

    I hope that helps.

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

Sidebar

Related Questions

I just started studying programming about 6 months ago and I have really been
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have just started learning Erlang and am trying out some Project Euler problems
I have just started using SlickGrid and was hoping someone can help me solve
I have just started studying Android, I have limited java knowledge but am semi
I have just started studying Python . I am referring to the tutorials at
I have just started studying Android and I am still lost. I would just
I have just started self-studying the Dragon book of Compiler Design. I am working
I have just started making a test app , here is what happened. I
I have just started studying OOP and I am a little confused of the

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.