NSMutableArray does not add anything with addObjectsFromArray

I am testing an AI for a card game and had problems when I noticed that the count method of the NSMutableArray object never increases after using its addObjectsFromArray method. I not only use addObjectsFromArray in the test, but in the AI โ€‹โ€‹method add NSArray (or NSMutableArray ), and in both cases nothing is added. In the test, I create a complete deck of 52 cards, shuffle, and then deal cards to 3 AI and an additional NSMutableArray . Then I see which cards the AI โ€‹โ€‹already has, and then see what each AI will play on its own for the value, and then add the cards that he played in his hand before moving on to the next AI.

Here is the code:

for(int k = 1; k < 14; k++) { NSLog(@"-|-|-|-|-|- Current Value: %i", k); NSLog(@"----- Easy"); [discard addObjectsFromArray:(NSArray*)[easy playHand:(NSUInteger)k]]; NSLog(@"Discard Count: %i", [discard count]); for(Card* cd in discard) { NSLog(@"card displayed = true"); [cd displayCard]; NSLog(@"card displayed = true"); } //[discard makeObjectsPerformSelector:@selector(displayCard)]; [easy addCards:discard]; [discard removeAllObjects]; NSLog(@"------ Medium"); blah = (NSArray*) [Medium playHand:k]; [discard addObjectsFromArray:blah]; NSLog(@"Discard Count: %i", [discard count]); //[discard addObjectsFromArray:[Medium playHand:(NSUInteger)k]]; for(Card* cd in discard) { [cd displayCard]; } //[discard makeObjectsPerformSelector:@selector(displayCard)]; [Medium addCards:discard]; [discard removeAllObjects]; NSLog(@"------ Hard"); [discard addObjectsFromArray:[HARD playHand:(NSUInteger)k]]; for(Card* cd in discard) { [cd displayCard]; } //[discard makeObjectsPerformSelector:@selector(displayCard)]; [HARD addCards:discard]; [discard removeAllObjects]; NSLog(@" "); NSLog(@" "); } 

Here the drop should capture cards that play playHand, which returns an NSMutableArray , and addObjectsFromArray accepts an NSArray (all pointers, of course). Should I not pass them directly using the following line?

 [discard addObjectsFromArray:[easy playHand:k]]; 

Quick crawls do not work using a separate pointer:

 NSArray* blah = nil; blah = [easy playHand]; [discard addObjectsFromArray:blah]; 

... or typecasting with a pointer (I tested this and it didn't work):

 NSArray* blahblah = nil; blahblah = (NSArray*)[easy playHand:k]; [discard addObjectsFromArray:blahblah]; 

What am I doing wrong? Isn't this automatic casting, or do I need to use a different form of casting? Or is there something else that I forget? I will try to force it to return an NSArray , but the container in the AI โ€‹โ€‹class is NSMutableArray , so I can easily add maps to it without making copies of arrays every time (a) a map object is added / deleted. (PS: Yes, everything was created correctly in advance, I just cut off this part because it made the code fragment rather long, and the additional NSMutableArray holding the remaining 13 cards has a return counter of 13 using addObject, but resetting the counter never changes from 0 .)

+4
source share
1 answer

A common problem is that discard is nil . I do not see this being initialized, so I think it is here.

0
source

All Articles