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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:55:52+00:00 2026-06-17T11:55:52+00:00

Firstly, an apology if this is a rather basic question, however I am a

  • 0

Firstly, an apology if this is a rather basic question, however I am a newcomer to Objective C and I have not been able to find an answer to my question in all of my searches (although this may well be because I am a novice not quite using the correct terminology while looking for an answer!)

I have a simple loop, inside of which I populate an NSMutableArray with some custom objects. What I find is that after the loop, I have an array whose elements all seem to have the same values: i.e. after inserting (0,1,2,3) I find that my array contains (3,3,3,3).

The problem is only present when using my objects; a second array, into which I add only strings, ends up populated with the expected values.

Looking at the addresses of the objects in my array, I see that they are all different: am I correct in thinking that this means that I do not simply have an array of pointers that reference the same object?

I am wanting to understand why this is not behaving as I expected. I have a feeling that there is something quite basic that I am missing, and any insights would be greatly appreciated.

This is the loop that I execute:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"STARTING");
    NSMutableArray* myArray1 = [NSMutableArray array];
    NSMutableArray* myArray2 = [NSMutableArray array];

    for (int i=0; i < 4; i++) {
        MyObject* obj = [[MyObject alloc] initWithSomeString: [NSString stringWithFormat: @"ID%u", i] 
                                                  AndSomeNum: i];
        [myArray1 addObject: obj];
        [myArray2 addObject: [NSString stringWithFormat: @"ID%u", i]];

        NSLog(@"  Added to array1: %@", [myArray1 objectAtIndex: i]);
        NSLog(@"       and array2: %@", [myArray2 objectAtIndex: i]);
    }

    NSLog(@"ALL DONE:\nARRAY1=%@\nARRAY2=%@", myArray1, myArray2);
}

The output that I get after running this is:

STARTING
   Added to array1: num=0 self=<0x928e9b0> str=<0x9290090> str=ID0
        and array2: ID0
   Added to array1: num=1 self=<0x9290080> str=<0x928ff80> str=ID1
        and array2: ID1
   Added to array1: num=2 self=<0x928eb00> str=<0x9290020> str=ID2
        and array2: ID2
   Added to array1: num=3 self=<0x7499a30> str=<0x7499ae0> str=ID3
        and array2: ID3
ALL DONE:
ARRAY1=(
    "num=3 self=<0x928e9b0> str=<0x7499ae0> str=ID3",
    "num=3 self=<0x9290080> str=<0x7499ae0> str=ID3",
    "num=3 self=<0x928eb00> str=<0x7499ae0> str=ID3",
    "num=3 self=<0x7499a30> str=<0x7499ae0> str=ID3"
)
ARRAY2=(
    ID0,
    ID1,
    ID2,
    ID3
)

This is the implementation of the MyObject class:

@implementation MyObject

NSString* _blah;
int _num;

-(MyObject*)initWithSomeString: (NSString*)blah AndSomeNum: (int)num {
    self = [super init];
    _blah = [NSString stringWithString: blah];
    _num = num;
    return self;
}

-(NSString*)description {
    return [NSString stringWithFormat: @"num=%u self=<%p> str=<%p> str=%@", _num, self, _blah, _blah];
}

@end
  • 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-17T11:55:53+00:00Added an answer on June 17, 2026 at 11:55 am

    The issue here is your definition of these two variables:

    NSString* _blah;
    int _num;
    

    Where you have them placed in this file, they are defined as global variables. So every instance of your class is sharing these same two variables. This is why each time you call your initializer, the same ‘_blah’ is being overwritten.

    If you meant for them to be instance variables, each instance having its own ‘_blah’ and ‘_num’, then you need to declare them within {} in the implementation or interface for your class. So this would work as an example:

    @implementation MyObject {
    
     NSString* _blah;
     int _num;
    
    } // continue with method definitions...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly sorry if this is a common question but I couldn't find anything on
Firstly I would say that I have read this post however I still have
Firstly, very sorry if this is not a true stackoverflow question. But it's something
Firstly, this is not a question about repository synchronisation for which there are numerous
Firstly, this seems like something that should have been asked before, but I cannot
Firstly, I know this [type of] question is frequently asked, so let me preface
(Firstly, as a disclaimer, this is related to an assignment. I'm not asking anyone
Firstly, an apology for the length of this post. If brevity is the soul
Firstly, I have migrated my CVS repository into SVN repository. I checked out this
Firstly anyone else trying to get onto forum.hibernate.org. I have been trying for a

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.