Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-16-2009, 09:37 PM   #1 (permalink)
Mark
 
Join Date: Aug 2009
Location: Portland OR
Posts: 2
Send a message via Skype™ to markhillig
Question NSMutableArray entries all the same.

I am new to cocoa and new to this forum so forgive me if I seem rather confused. I am working on a simple card game. I have a function in my viewController class that is intended to populate an NSMutableArray with objects corresponding to a class I created to represent each card (deck.m). I use a 'deck' object variable to create each card and then store it in the array. The problem is that each time I use addObject to add the new card to the array all of the objects in the array are changed to the new object being added. I think this is because the deck object used to create the new card is recycled. It appears that each entry in the array is pointing to this one deck variable. My question is, what can I do differently to prevent all the objects in the array from changing? Here is the function in question:
Code:
- (NSMutableArray *)stackTheDeck {

	deck *currentCard = [[deck alloc] init];
	NSMutableArray *currentDeck = [[NSMutableArray alloc] init];
	//NSMutableArray *currentDeck = [NSMutableArray arrayWithCapacity:0];
	
	int i = 0;
	
	for(i = 1; i < 53; i++) {
		//Set the state of each card
		[currentCard setPlayed:[NSString stringWithFormat:@"false"]];
		
		//Set the card value
		if((i % 13) == 10) {
			[currentCard setCard:[NSString stringWithFormat:@"J"]];
		}
		else if ((i % 13) == 11) {
			[currentCard setCard:[NSString stringWithFormat:@"Q"]];
		}
		else if ((i % 13) == 12) {
			[currentCard setCard:[NSString stringWithFormat:@"K"]];
		}
		else if ((i % 13) == 0) {
			[currentCard setCard:[NSString stringWithFormat:@"A"]];
		}
		else {
			[currentCard setCard:[NSString stringWithFormat:@"%d", (i%13)]];
		}
		
		//Set the suite for the card
		switch (i/13){
			case 0:
				[currentCard setSuite:[NSString stringWithFormat:@"diamond"]];
				break;
			case 1:
				[currentCard setSuite:[NSString stringWithFormat:@"heart"]];
				break;
			case 2:
				[currentCard setSuite:[NSString stringWithFormat:@"spade"]];
				break;
			case 3:
				[currentCard setSuite:[NSString stringWithFormat:@"club"]];
				break;
			default:
				break;
		}
		//[decks insertObject:currentCard atIndex:(i-1)]; //Add the card to the deck
		[currentDeck addObject:currentCard]; //Add the card to the deck
		cardLabel.text = [NSString stringWithFormat:@"%@ - %@ - %@",[currentCard card], [currentCard suite], [currentCard played]];
	}
	[currentCard release];
		NSLog([NSString stringWithFormat:@"currentDeck contains %d records.",[currentDeck count]]);
	return [currentDeck autorelease];
}
Thanks for looking.
__________________
- Mark
markhillig is offline   Reply With Quote
Old 08-17-2009, 12:50 AM   #2 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
Send a message via AIM to kelvinkao
Default

You are allocating just one card and then adding it to the deck 52 times. You should have the allocation inside the loop so you can add a different card each time.
kelvinkao is offline   Reply With Quote
Old 08-17-2009, 01:47 AM   #3 (permalink)
Mark
 
Join Date: Aug 2009
Location: Portland OR
Posts: 2
Send a message via Skype™ to markhillig
Default

Quote:
Originally Posted by kelvinkao View Post
You are allocating just one card and then adding it to the deck 52 times. You should have the allocation inside the loop so you can add a different card each time.
That seems to have resolved the issue. Although I now see some other mistakes I have made. Thanks for the quick assistance! It is always nice to see there are friendly helpful folks when you are new to a forum.
__________________
- Mark
markhillig is offline   Reply With Quote
Reply

Bookmarks

Tags
game, nsmutablearray

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 284
20 members and 264 guests
ADY, AragornSG, BrianSlick, Dani77, Dattee, dre, glenn_sayers, HDshot, HemiMG, JasonR, karlam963, nobre84, prchn4christ, Rudy, spiderguy84, themathminister, tomtom100, viniciusdamone, vogueestylee, vvenkatachallam
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,884
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, karlam963
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:18 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0