0

I have a very strange issue in iOS.I have two classes AViewController & BViewController. In AViewController i am having an array like

@property NsmutableArray *array_one;

I am passing some data from one view controller to another in prepareforsegue method.

 if([self.array_one count]>0)
        {

            viewBController.array_two=self.array_one;

        }

then from BviewController i am passing back the data like this

-(void)viewWillDisappear:(BOOL)animated
{


    int j;


    [self.delegate setData:self.array_two];

}

Then i am in this method doing this work

- (void)setData:(NSMutableArray *)data
{
    NSString *string=[NSString stringWithFormat:@"%lu",(long)[data count]];
   [self.array_one removeAllObjects];
    int i;

    for(i=0;i<[string intValue];i++)
    {
        User *u=[data objectAtIndex:i];
        [self.array_one addObject:u];
    }



}

Now what happened is if go to view one controller to another 2 times in second time if i add this line [self.array_one removeAllObjects]; then my data array get empty.I don't know why? Please guide me

6
  • 2
    Sweet combo of NSString *string=[NSString stringWithFormat:@"%lu",(long)[data count]]; and [string intValue]. Well done. Commented Sep 30, 2015 at 14:51
  • You only have one actual array, right? You have two pointers to it (array_one, array_two) but whether you remove objects using one name or another, it's still removing them from a single array. Commented Sep 30, 2015 at 14:55
  • @Droppy Sarcasm is wasted on SO. Commented Sep 30, 2015 at 14:56
  • In case you missed Droppy's point, in your setData: method, get rid of the string variable. Change your for loop to for (NSUInteger i = 0; i < data.count; i++). Better yet, replace the whole loop with just [self.array_one addObjectsFromArray:data];. Commented Sep 30, 2015 at 15:06
  • 1
    Never take advice from that person again. Commented Sep 30, 2015 at 15:28

1 Answer 1

2

You use two different methods of passing the data.

viewBController.array_two=self.array_one;

This assigns the array_two pointer to be the same as array_one. This means that they both point to the same array.

Then, when you call [self.delegate setData:self.array_two], the first thing that happens is that you call [self.array_one removeAllObjects]. But this is pointing to the same array as array_two, so you will empty the array.

If the data is manifestly the same at all times between the two classes, one solution would just be to assign the array to both view controllers once, after which changes in one will be reflected in the other.

If not, then you need to copy the array to a new object when you assign to the second view controller.

Sign up to request clarification or add additional context in comments.

9 Comments

I guess you are right but the array data it just an argument so this should not get empty.I am passing self.array_two in stead as an argument
You are not passing the whole array, you are passing a pointer to the array. If you don't understand what that means, I suggest looking at some tutorials.
suggest me some please
I think issue is when i write viewBController.array_two=self.array_one; then array is not getting copied their pointer is copied and when use [self.delegate setData:self.array_two] then the pointer is passed as an argument which points to one array & when i do [self.array_one removeAllObjects]. then it empty the array so all three of them were pointing to same array that is why it made an array empty.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.