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 01-10-2010, 02:43 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 31
Question if statements and table views problem

Hi, I have been having with implementing a function in a table view sports app. my app is a schedule and im trying implement a today navbar button. this button will go to the cell of the game when it is the right date. I believe that the way i am getting the date is good

Code:
self.dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[self.dateFormatter setDateStyle:NSDateFormatterShortStyle];
[self.dateFormatter setTimeStyle:NSDateFormatterNoStyle];

//gameDay is a NSString
gameDay= [self.dateFormatter stringFromDate:[NSDate date]];
but when i go to do some if statements only the first one works when i use one equal sign

Code:
int row;	
	
if (gameDay = @"09-01-10") {
	row = 46;
	[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
	}
	
	else if (gameDay = @"14-01-10") {
		row = 47;
		[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
	}
and when i use two equal signs

Code:
int row;	
	
	if (gameDay == @"09-01-10") {
		row = 46;
		[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
	}
	
	else if (gameDay == @"14-01-10") {
		row = 47;
		[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
	}
nothing happens even though i know that two equal should be right.

I want to know why it only works for the first one with one equal but not for the one, in my opinion is right, with 2 equals.

A solution for this problem would be really great because i have trying to do two functions like this and they dont work.

thanks
pete42 is offline   Reply With Quote
Old 01-10-2010, 05:41 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Code:
if ([aString isEqualToString: anotherString])
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

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

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 01-10-2010, 05:46 PM   #3 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: London
Posts: 226
Default

In your example code, gameDay and @"09-01-10" are both examples of NSString objects. In order to compare them, you need to use one of their comparison functions. eg.
Code:
if( [gameDay isEqualToString: @"09-01-10"] ) { ... }
Basically, this is sending one string the -isEqualToString: message, along with another string to compare it to. This returns YES or NO, depending on whether they're equal.

(You could also do [@"09-01-10" isEqualToString: gameDay] ... it's the same difference.)

'=' is the assignment operator. In your first example, you're assigning the address of the object @"09-01-10" to the variable gameDay. (Since this was non-zero, this also evaluated to the same as YES, which is why the code was run.)

'==' is the comparison operator, but it only works on C primitives. In this case, you were comparing the ADDRESSES where the two strings were held in memory, which were different, which is why it was failing.

EDIT: Sorry, BrianSlick, you answered while I was still typing
__________________
SimCap - Simple iPhone and iPad Simulator screen capture
_sjc_ is offline   Reply With Quote
Old 01-10-2010, 06:21 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 31
Default

that seems like it would work but it just crashes the app every time. this is the whole code im using:

Code:
-(void) today:(id)sender {
	
	int row;	

	if ([gameDay isEqualToString:@"10-01-09"]) {
		row = 46;
		[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
	}
	
	else if ([gameDay isEqualToString: @"10-01-14"]) {
		row = 47;
		[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
	}
	
	 
	else {
	 
	 UIAlertView *myAlert2 = [[UIAlertView alloc] initWithTitle:@"No Game Today" message:@"The Next Game is on\nJanuary 14, 2010" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
	 [myAlert2 show];
	 [myAlert2 release];
	 
}
}
I always get "EXC_BAD_ACCESS"
pete42 is offline   Reply With Quote
Old 01-10-2010, 06:24 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: London
Posts: 226
Default

Are you retaining gameDay? stringFromDate: returns an autoreleased string object, which means that by the time you come to compare against it, it will probably have been disposed of. Try:
Code:
gameDay= [[self.dateFormatter stringFromDate:[NSDate date]] retain];
__________________
SimCap - Simple iPhone and iPad Simulator screen capture
_sjc_ is offline   Reply With Quote
Old 01-10-2010, 06:27 PM   #6 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 31
Default

thanks you so much its working.
pete42 is offline   Reply With Quote
Old 01-10-2010, 06:27 PM   #7 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Better yet, use @properties.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

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

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Reply

Bookmarks

Tags
table cell, table view, tables, tableview, tableviewcontroller

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: 248
23 members and 225 guests
ADY, AragornSG, bookesp, chillyh, dacapo, Dani77, Davey555, Dominus, dre, glenn_sayers, HemiMG, JasonR, karlam963, M.A.S., marshusensei, mer10, nobre84, Oral B, prchn4christ, Raggou, Rudy, spiderguy84, themathminister
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,765
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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