Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding values to two different NSMutableArray without increasing the retain count
    primarykey
    data
    text
    <p>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. </p> <p>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.</p> <p>For example,</p> <p>Let's say we have two mutable NSArray:</p> <pre><code>NSMutableArray *mutableArrayOne; NSMutableArray *mutableArrayTwo; </code></pre> <p>Now let's create the object definition that these array needs to contain.</p> <pre><code>@interface: DummyObject : NSObject { int objectValue; } @property (nonatomic) int objectValue; -(void) printObjectValue; @end </code></pre> <p>Now let's create the base class to store the arrays. </p> <p><strong>Base Class Definition</strong> </p> <pre><code>@interface: BaseClass : NSObject { NSMutableArray *mutableArrayOne; NSMutableArray *mutableArrayTwo; } -(void) init; -(void) printBothArrays; @end </code></pre> <p><strong>Base Class Implementation</strong> </p> <pre><code>@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 </code></pre> <p>Now let me the modify the DummyObject in array One:</p> <pre><code>for (DummyObject* dummyObject in mutableArrayOne) { [dummyObject setValue:100]; } </code></pre> <p><strong>Problem</strong> Now here starts the problem when I am printing the values for both array objects:-</p> <h1>Printing First Array</h1> <pre><code>for (DummyObject* dummyObject in mutableArrayOne) { [dummyObject printObjectValue]; } </code></pre> <p>*<em>Output Log (from first array) *</em></p> <pre><code>100 100 </code></pre> <h1>Printing second Array</h1> <pre><code>for (DummyObject* dummyObject in mutableArrayTwo) { [dummyObject printObjectValue]; } </code></pre> <p>*<em>Output Log (from second array) *</em></p> <pre><code>2 </code></pre> <p>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.</p> <p>How can we do that? Do we any other alternative?</p> <p>Thanks, <strong>Paras Mendiratta</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload