moalemadi
moalemadi

Reputation: 57

iphone- Can't remove objects from array

My app is nearly finished but I am having a problem, I can not remove objects from an array I made.

This is a sample of my code:

-(void) TheGame{

  squares = [[NSMutableArray arrayWithObjects: @"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h" ,@"i" ,@"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r", @"s", @"t", @"aa", @"bb", @"cc",@"dd", @"ee", @"ff", @"gg", @"hh", @"ii", @"jj", @"kk", @"ll", @"mm", @"nn", @"oo", @"pp", @"qq",@"rr", @"ss", @"tt", nil] retain];

  s=40;
  turn=1;

  while(s >= 1)
    if (turn == 1){
      //Check for players move
      //Here I added some if statement, and for every one I added 
      //[squares removeObject: @"b"]; X can be any object of the array.
      turn = 0;
      s = s - 1;
    }

  if (turn == 0){

    //Here the computer should find an object from the array randomly. I used
    //randomN = [squares objectAtIndex:arc4random() % [squares count]];
    //to choose the random object.

    //After choosing, the item should be removed from the array, also using
    //[squares removeObject: @"X"]; X can be any object of the array.

    turn = 1;
    s = s - 1;
  }
}

The problem is, for example, if the player chooses the object "b" from the array, then

[squares removeObject: @"b"];

should trigger.

when it comes to the computer random move, there is a chance that it picks the same object which was already removed from the array, which is @"b" in my example.

So that means, the items are not being removed from the array when

[squares removeObject: @"X"];

triggers.

Any one got a solutions for this problem ?

Please,, I need your help.

======= SOLVED ========

I tried to use your answers but could not manage to solve the problem..

The solution was so simple :p .. Just moved the array to "viewDidLoad" method.

Thanks guys for your help, so sorry but I can't accept answers as non worked for me. I appreciate you effort.

Upvotes: 1

Views: 300

Answers (3)

Alan Zeino
Alan Zeino

Reputation: 4396

Your original question is unclear, but I think you're having problems with the age-old issue of pointer referencing.

The following is a typical example of two strings that are the same, yet are not exactly the same:

if (@"bears" == @"bears")
{

}

the above only compares pointers, not strings. Saying something like [squares removeObject:@"b"]; is incorrect, because you're allocating a new string (b) and then saying to your Array; "remove the object referenced by this pointer", when what you've been expecting is "remove the object that contains a b in it" (which is not how Arrays work).

tl;dr, use a Dictionary.

Upvotes: 1

Gal Blank
Gal Blank

Reputation: 2090

I would use NSMutableDictionary instead of Array that way you can have a direct access for adding/removing etc...

Upvotes: 1

You might try using indexOfObject: then removeObjectAtIndex: to remove an item from the array.

I suspect that if you debug, indexOfObject might give you a result of NSNotFound (very large number) which means that for some reason it would think the array did not hold the string you are passing in. Very probably something in the string you are passing in to remove an entry from the array is not matching with the equivalent entry in the array.

Upvotes: 2

Related Questions