using NSMutableArray
Hi,
First time posting to an online forum. Basically I've got a function, checkPileCardNum that iterates through calling another function populateArrayUpToPileCard until the function populateArrayUpToPileCard returns YES.
Each time populateArrayUpToPileCard is called it checks an NSArray of UIImages to see if the image associated with the index number passed into the array isEqual to the Image of a separate UIImageView, named pile. If the image is the same it returns YES and exits both the populateArrayUpToPileCard and checkPileCardNum function. If the image is different it initiates an NSMutableArray, called tempArray and adds the image to the array.
My aim is that the functions should create an Array of images up until the same image as the one used on the pile object, which I can then check another UIImage against to see if it is in the new array.
The function which calls the two other functions is called checkCardAgainstPile and I've setup a label to displays how many items are in the new tempArray. When I call it however it only populates 1 image and immediately afterwards returns YES.
The dealCards singleton class created works fine everywhere else in the application, it seems to be something in the populateArrayUpToPileCard function.
My code is attached below, any help whatsoever would be greatly appreciated.
- (void)checkCardAgainstPile
{
[self checkPileCardNum]; //call checkPileNum
int temp = [tempArray count];
NSString *cardStacktext = [[NSString alloc] initWithFormat: @"%d", temp]; //display number of items on UILabel
cardsInDeck.text = cardStacktext;
}
-(void)checkPileCardNum
{
for (int cardNum = 0; [self populateArrayUpToPileCard:cardNum] == YES; cardNum++) //iterate through calling populateArrayUpToPileCard each time, until (populateArrayUpToPileCard == YES)
{
[self populateArrayUpToPileCard:cardNum];
cardNum++;
[self checkPileCardNum];
}
}
-(BOOL)populateArrayUpToPileCard:(int)cardNum
{
if ([[[dealCards sharedDealData].deck objectAtIndex:cardNum] isEqual: [pile image]]) //check to see if image is the same as Pile image
{
return YES;
} else {
if (tempArray == nil) { //if not allocate tempArray
tempArray = [[NSMutableArray alloc] init];
}
[tempArray addObject:[[dealCards sharedDealData].deck objectAtIndex:cardNum]]; //add item to tempArray
return NO;
}
}
Last edited by robbiecutting; 05-12-2011 at 07:55 PM.
|