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

  • Home
  • SEARCH
  • 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 7908593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:01:06+00:00 2026-06-03T12:01:06+00:00

I have two simple NSMutableArray that consists of few objects. Some of these objects

  • 0

I have two simple NSMutableArray that consists of few objects. Some of these objects can be common but need to be stored in both of the arrays as uses of both arrays are defined for totally different purpose.

However, the problem is that after adding same objects to both array, on changing the value of one of the common object, does not reflect in 2nd array.

For example,

Let’s say we have two mutable NSArray:

NSMutableArray *mutableArrayOne;
NSMutableArray *mutableArrayTwo;

Now let’s create the object definition that these array needs to contain.

@interface: DummyObject : NSObject
{
     int objectValue;
}

@property (nonatomic) int objectValue;

-(void) printObjectValue;

@end

Now let’s create the base class to store the arrays.

Base Class Definition

@interface: BaseClass : NSObject
{
    NSMutableArray *mutableArrayOne;
    NSMutableArray *mutableArrayTwo;
}
-(void) init;
-(void) printBothArrays;

@end

Base Class Implementation

@implementation BaseClass

-(void) init
{
     // initialize the mutable array.
     mutableArrayOne = [[NSMutableArray alloc] initWithCapicity:5];
     mutableArrayTwo = [[NSMutableArray alloc] initWithCapicity:5];

     DummyObject *dummyObject = [DummyObject alloc];
     [dummyObject setObjectValue:5];

     DummyObject *dummyObjectTwo = [DummyObject alloc];
     [dummyObjectTwo setObjectValue:2];

     [mutableArrayOne addObject:dummyObject];
     [mutableArrayOne addObject:dummyObjectTwo];

     [mutableArrayTwo addObject:dummyObjectTwo];

}

@end

Now let me the modify the DummyObject in array One:

for (DummyObject* dummyObject in mutableArrayOne)
{
    [dummyObject setValue:100]; 
}

Problem
Now here starts the problem when I am printing the values for both array objects:-

Printing First Array

for (DummyObject* dummyObject in mutableArrayOne)
{
    [dummyObject printObjectValue]; 
}

*Output Log (from first array) *

100
100

Printing second Array

for (DummyObject* dummyObject in mutableArrayTwo)
{
    [dummyObject printObjectValue]; 
}

*Output Log (from second array) *

2

So here we can see that MutableArray is keeping the copy of the object, however, I want to store only the reference. That means, on changing the value of the object in 1st array should reflect in 2nd array.

How can we do that?
Do we any other alternative?

Thanks,
Paras Mendiratta

  • 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-03T12:01:07+00:00Added an answer on June 3, 2026 at 12:01 pm

    This will ideally work, seems like some issue with setting object.
    For e.g. you can check like this –

    NSMutableString *firstString = [[NSMutableString alloc] initWithString:@"first"];
    NSMutableString *secondString = [[NSMutableString alloc] initWithString:@"second"];
    
    NSMutableArray *originalArray = [[NSMutableArray alloc] initWithObjects: firstString, secondString, nil];
    NSMutableArray *copyArray = [[NSMutableArray alloc] initWithObjects: firstString, secondString, nil];
    
    [[copyArray objectAtIndex:0] appendString:@"add some text"];
    
    for (int index = 0; index < [originalArray count]; index++) {
        NSLog(@"Original:%@ --- copy:%@", [originalArray objectAtIndex:index], [copyArray objectAtIndex:index]);
    }
    

    And output is –

    2012-05-09 10:28:40.382 Demo[5237:f803] Original:firstadd some text --- copy:firstadd some text
    2012-05-09 10:28:40.384 Demo[5237:f803] Original:second --- copy:second
    

    EDIT – (Not adding object at the time of initialization)

    NSMutableString *firstString = [[NSMutableString alloc] initWithString:@"first"];
    NSMutableString *secondString = [[NSMutableString alloc] initWithString:@"second"];
    
    NSMutableArray *originalArray = [[NSMutableArray alloc] init];
    NSMutableArray *copyArray = [[NSMutableArray alloc] init];
    
    [originalArray addObject:firstString];
    [copyArray addObject:firstString];
    
    [originalArray addObject:secondString];
    [copyArray addObject:secondString];
    
    [[copyArray objectAtIndex:0] appendString:@"add some text"];
    
    for (int index = 0; index < [originalArray count]; index++) {
        NSLog(@"Original:%@ --- copy:%@", [originalArray objectAtIndex:index], [copyArray objectAtIndex:index]);
    }
    

    Output –

    2012-05-09 11:11:18.275 Demo[5433:f803] Original:firstadd some text --- copy:firstadd some text
    2012-05-09 11:11:18.277 Demo[5433:f803] Original:second --- copy:second
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these two simple files that define a C++ class with a tryME
I have two simple tables in my database. A card table that contains Id,
I have two simple while loops in my program that I feel ought to
I have two very simple, identical UITableViews in my app that are populated with
Simple idea: I have two images that I want to merge, one is 500x500
I have two webapplication, one is a simple authenticationsite which can authenticate the logged
I have a very simple model with two objects: Name and Category. One Name
I have two simple script that enables/disables Cisco AnyConnect when I don't want it
Currently, I have edited a delegate function that adds Exercise objects to an NSMutableArray.
I have two simple questions that concern the exceptions in Symfony2. Here follows 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.