0

I have following code :

- (IBAction)goButton:(id)sender 
{

    if(buttonCount==0)
    {
        previousStateArray=setUpArray;
          NSLog(@"previous array count=%d",[previousStateArray count]);
        [setUpArray removeAllObjects];
        for(Node *n in nodeArray)
        {
            if(![temp isEqualToString:n.strName])
            {
                [setUpArray addObject:n.strName];

            }
            temp=n.strName;         
        }    
    }
}  

- (IBAction)backButton:(id)sender 
{

    [setUpArray removeAllObjects];
    setUpArray=previousStateArray;
     NSLog(@"previous array count=%d",[previousStateArray count]);
    buttonCount--;       
  }

Both setUpArray and previousStateArray are declared in the -viewDidLoad method.My view is loading only once.In first NSLog i am getting 1 as a output but in second NSLog i am getting

0 as a output while none of my array initialize again . so why this is happening???

1
  • which array count is 1 and u get zero? previous? Commented May 14, 2012 at 6:58

5 Answers 5

1

Your problem is with your array pointers.

In goButton :

previousStateArray=setUpArray;

Now previousStateArray points to the same array setUpArray is pointing to.

[setUpArray removeAllObjects];

This removes all objects and now both pointers point to an empty array.

In backButton:

setUpArray=previousStateArray;

They both point to the same empty array so this line is redundant. You should keep a temporary pointer if you want to swap the pointers.

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

Comments

1

In your code

`previousStateArray=setUpArray;`  //previousStateArray pointing to same memory location of setUpArray.

Use - (void)setArray:(NSArray *)otherArray method of NSMutableArray class.

[previousStateArray setArray: setUpArray];
[setUpArray setArray:previousStateArray];  

- (IBAction)goButton:(id)sender 
{

    if(buttonCount==0)
    {
        [previousStateArray setArray: setUpArray];
          NSLog(@"previous array count=%d",[previousStateArray count]);
        [setUpArray removeAllObjects];
        for(Node *n in nodeArray)
        {
            if(![temp isEqualToString:n.strName])
            {
                [setUpArray addObject:n.strName];

            }
            temp=n.strName;         
        }    
    }
}  

- (IBAction)backButton:(id)sender 
{

    [setUpArray removeAllObjects];
    [setUpArray setArray:previousStateArray];
     NSLog(@"previous array count=%d",[previousStateArray count]);
    buttonCount--;       
  }

Comments

0

Initialize the array

setUpArray = [[NSMuatableArray alloc] init];

Comments

0

I think giorashc's answer is correct. Your problem is using "=" for Array only makes the new one previousStateArray points to the same memory of setUpArray. In other words, previousStateArray is the same as setUpArray .

For more detail information , you should search deep & shallow copy.

Comments

-1

You should also use if(0 == buttonCount) rather than if(buttonCount==0).

Comments

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.