AAA.m:
- (void)keepCurrentArray:(id)object
{
_currentTest=[[NSMutableArray alloc]init];
[_currentTest addObject:@"one"];
[_currentTest addObject:@"two"];
[_currentTest addObject:object];
NSLog(@"My Array is:%@",_currentTest);
}
Class BBB.m is passing objects to class AAA.
Right now if i’m passing X to the above method so the array will be: one,two,X . Then i’ll send it Y and the array will be one,two,Y instead of what i want to accomplish: one,two,x,one,two,y.
Is that because I’m alloc and init _currentTest every time? How can I solve it?
Update:
I had a few suggestions on how to solve this and none of them worked for me. I’ve created a new project with just the code in the answers and i’m still getting the same result when I try to add the second object i get: one, two, test instead of one,two,test,one,two,test
_currentTest=[[NSMutableArray alloc]init];in a method is never a good thing!!!As per naming convention it seems to be a property to the AAA Class. So for property, the alloc+init should be either in
initorawakeFromNib. So that if is initialized just once.However in some situations init is called more than once then your previous values are lost and new set are added.
So what you can do is make another class and put this
_currentTestArray there and make it static and use it here. I hope this will work fine. And make sure in the init method of that class it is initialized just once, as :