NSMutableArray not adding anything with addObjectsFromArray
I am testing the AI for a card game and had issues when I noticed the count method of an NSMutableArray object was never increasing after using its addObjectsFromArray method. Not only am I using addObjectsFromArray in the test, but in a method for the AI to add an NSArray (or NSMutableArray), and in both cases, nothing is being added. In the test, I create a full, 52-card deck, shuffle, then deal the cards out to 3 AI's and an extra NSMutableArray. Then I see what cards the AI already has, then see what each AI would play on its turn for a value, then add the cards it played back into its hand before moving on to the next AI.
Here's 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's 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开发者_Python百科:discard];
[discard removeAllObjects];
NSLog(@"------ Medium");
blah = (NSArray*) [Medium playHand:k];
[discard addObjectsFromArray:blah];
NSLog(@"Discard's 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(@" ");
}
From here, discard should be catching the cards played by playHand, which is returning an NSMutableArray, and addObjectsFromArray takes an NSArray (all pointers, of course). Should I not be passing them directly using the line below?
[discard addObjectsFromArray:[easy playHand:k]];
The quick work-arounds aren't working either, with 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? Is this not an automatic typecasting, or do I need to use another form of typecasting? Or is there something else I'm overlooking? I'll try to have it return an NSArray instead, but the container in the AI class is a NSMutableArray so I can add cards to it easily without creating copies of arrays every time a card object(s) is(are) added/removed. (PS: Yes, everything is properly created beforehand, I just snipped that part off because it made the code snippet pretty long. And the extra NSMutableArray holding the remaining 13 cards has count return 13 using addObject, but discard's count never changes from 0.)
The usual problem is that discard
is nil
. I don't see it being initialized so I am guess that is the case here too.
精彩评论