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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 07-07-2010, 05:59 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 651
kapps11 is on a distinguished road
Default NSMutableArray Problems

Hey guys -

So in this app that im working on, I have an NSMutableArray that holds different colors. And yet i find that the count of the array is changing at random times. Ex: I log the count when its first set, and its 5, but then when i need to compare it to something else, for some reason it is now 4. No calls have been made to the array in between those times. any ideas?

Thx
kapps11 is offline   Reply With Quote
Old 07-07-2010, 06:09 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Post some code.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 07-07-2010, 06:50 PM   #3 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 651
kapps11 is on a distinguished road
Default

Code:
int color = arc4random() % 4;
	switch (color) {
		case 0:
			[self.sequenceArray addObject:[[UIColor redColor] retain]];
			break;
		case 1:
			[self.sequenceArray addObject:[[UIColor yellowColor] retain]];
			break;
		case 2:
			[self.sequenceArray addObject:[[UIColor greenColor]retain]];
			break;
		case 3:
			[self.sequenceArray addObject:[[UIColor blueColor] retain]];
			break;
		default:
			break;
	}
that adds a color to the sequence. So i log after that function and it counts as 5. Then I log the same statement later and it counts as 4. None of the other code sets anything in the array, just reads it to compare to numbers etc so idt its helpful Ex:
Code:
if ([[object.sequenceArray objectAtIndex:index] isEqual:[UIColor redColor]])
kapps11 is offline   Reply With Quote
Old 07-07-2010, 06:58 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

This is going to take a while if you only post snippets. Post everything - logs, whatever - related to that array if you want any chance of figuring out the issue.

And you are leaking the color objects, btw.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 07-07-2010, 07:05 PM   #5 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 651
kapps11 is on a distinguished road
Default

Well I thought that maybe the color objects were being released or something so I retained them. alright here u go:

Code:
- (IBAction)colorPushed:(UIButton *)sender {
	if (!gameOver) {
	if (player1move < [simon.sequenceArray count]) {
	switch (sender.tag) {
		case 1:
			if ([[simon.sequenceArray objectAtIndex:player1move] isEqual:[UIColor redColor]]) {
				NSLog(@"red correct");
				player1score++;
			}
			player1move++;
			break;
		case 2:
			if ([[simon.sequenceArray objectAtIndex:player1move] isEqual:[UIColor yellowColor]]) {
				NSLog(@"yellow correct");
				player1score++;
			}
			player1move++;
			break;
		case 3:
			if ([[simon.sequenceArray objectAtIndex:player1move] isEqual:[UIColor greenColor]]) {
				NSLog(@"green correct");
				player1score++;
			}
			player1move++;
			break;
		case 4:
			if ([[simon.sequenceArray objectAtIndex:player1move] isEqual:[UIColor blueColor]]) {
				NSLog(@"blue correct");
				player1score++;
			}
			player1move++;
			break;

		default:
			break;
	}
		if (player1score == [simon.sequenceArray count]) {
			if (!gameOver) {
			NSLog(@"Player 1 wins");
				winLabel.text = @"Player 1 Wins!";
			player1move = 0;
			player2move = 0;
			player1score = 0;
				player2score = 0;
				continueButton.hidden = NO;
			//[simon addStepToCurrentSequence];
				gameOver = YES;
				NSLog(@"Count %d", [simon.sequenceArray count]);
			}
			}
		}
	else {
		if (player2move >= [simon.sequenceArray count]) {
			self.continueButton.hidden = NO;
			winLabel.text = @"BOTH LOSE!";
		}
	}
	
	if (player2move < [simon.sequenceArray count]) {
		switch (sender.tag) {
			case 5:
				if ([[simon.sequenceArray objectAtIndex:player2move] isEqual:[UIColor redColor]]) {
					NSLog(@"red correct");
					player2score++;
				}
				player2move++;
				break;
			case 6:
				if ([[simon.sequenceArray objectAtIndex:player2move] isEqual:[UIColor yellowColor]]) {
					NSLog(@"yellow correct");
					player2score++;
				}
				player2move++;
				break;
			case 7:
				if ([[simon.sequenceArray objectAtIndex:player2move] isEqual:[UIColor greenColor]]) {
					NSLog(@"green correct");
					player2score++;
				}
				player2move++;
				break;
			case 8:
				if ([[simon.sequenceArray objectAtIndex:player2move] isEqual:[UIColor blueColor]]) {
					NSLog(@"blue correct");
					player2score++;
				}
				player2move++;
				break;
				
			default:
				NSLog(@"ERROR IN P2 SIWTCH");
				break;
		}
		if (player2score == [simon.sequenceArray count]) {
			if (!gameOver) {
			NSLog(@"Player 2 wins");
			winLabel.text = @"Player 2 Wins!";
			player1move = 0;
			player2move = 0;
			player1score = 0;
			player2score = 0;
				continueButton.hidden = NO;
				gameOver = YES;
				NSLog(@"Count %d", [simon.sequenceArray count]);
		}
		}
	}
	else {
		if (player1move  >= [simon.sequenceArray count]) {
			self.continueButton.hidden = NO;
			winLabel.text = @"BOTH LOSE!";
		}
	}

	NSLog(@"%d", player1move);
	NSLog(@"%d", player2move);
	}
}


- (IBAction)continueGame {
	gameOver = NO;
	winLabel.text = @"";
	[simon addStepToCurrentSequence];
	NSLog(@"Array Count %d", [simon.sequenceArray count]);
	NSLog(@"%@", simon.sequenceArray);
	
	[self viewDidLoad];
}
So basically theres 8 buttons, each with a different tag. that corresponds to a color. the simon object managaes the color array, and sets it using the code i posted earlier. Any ideas? In the logs, right after continueGame is called, it is logged as 5, but when its logged during the colorPushed function, its 4.
kapps11 is offline   Reply With Quote
Old 07-07-2010, 08:07 PM   #6 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Does this:

Code:
NSLog(@"%@", simon.sequenceArray);
...report what you expect? If so, add it to the other places where you log the count and see if you can figure out what is missing.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 07-08-2010, 12:02 AM   #7 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 651
kapps11 is on a distinguished road
Default

Thx! I figured it out. really stupid mistake on my part - I called viewDidLoad after adding to the sequence, and in there it starts a new sequence. So It would be 5 then, but then go back to (which is the beginning/default) when it starts a new sequence. Thanks for your help!
kapps11 is offline   Reply With Quote
Reply

Bookmarks

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: 345
14 members and 331 guests
akacaj, c2matrix, cgokey, esoteric, EXOPTENDAELAX, GHuebner, givensur, HemiMG, Mirotion22, mjnafjke, Pudding, SLIC, Sonuye857, Techgirl-52
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,652
Threads: 94,115
Posts: 402,887
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Sonuye857
Powered by vBadvanced CMPS v3.1.0

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