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 12-05-2010, 11:01 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 17
ArtandIllustration is on a distinguished road
Default Comparison to disable button

Hi All,

Trying to compare a date (shown in a label, ''fed'' by an NSDate) and a string I've specified in order to disable a button. The label shows the date correctly and there are no fatal errors but the button is unaffected. Any thoughts? I know this may seem basic so I apologise; I've only been learning a couple of weeks in spare time from various tutorials - seems like a giant.

NSDate *B = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; //NSDate from date
[dateFormatter setDateFormat:@"EEE"];
//Use label to check recept of Date
labelDate.text = [dateFormatter stringFromDate:[NSDate date]];


NSString *daytocompare = @"SAT";
labelShows.text =daytocompare;

if([daytocompare isEqualToString:labelDate.text]) {
buttontosatnoon.enabled=YES;
}
else{
buttontosatnoon.enabled= NO;



Any thoughts? Thank you!
ArtandIllustration is offline   Reply With Quote
Old 12-05-2010, 11:31 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Good job on giving an example of your problem that's easy to test and debug; that's key for getting a good answer from a forum. . I took out the labels and added some NSLogs so I could see what's happening:

Code:
-(void)test8{
	
	NSDate *B = [NSDate date]; //unused variable B
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; //NSDate from date
	[dateFormatter setDateFormat:@"EEE"];
	//Use label to check recept of Date
	NSString *labelDateText = [dateFormatter stringFromDate:[NSDate date]];
	
	
	NSString *daytocompare = @"SUN";
	
	NSLog(@"labelDateText %@",labelDateText);
	NSLog(@"daytocompare %@",daytocompare);
	
	BOOL datesMatch = [daytocompare isEqualToString:labelDateText];
	
	NSLog(@"datesMatch %d", datesMatch);
	
}
Here's my output:
Code:
2010-12-05 09:21:39.024 testProj2[2660:307] labelDateText Sun
2010-12-05 09:21:39.028 testProj2[2660:307] daytocompare SUN
2010-12-05 09:21:39.032 testProj2[2660:307] datesMatch 0
You can probably see the problem there - "Sun" will not match "SUN" because isEqualToString: is case sensitive. You need a case-insensitve compare.

Code:
BOOL datesMatch = ([datytocompare caseInsensitiveCompare:labelDateText] == NSOrderedSame);
That compares the two strings for sorting and tells you if they have the same ordering; that'll server your purpose too.

You could also change your string constants so they match the output of the formatter, or uppercase all of the strings before comparing.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 12-08-2010, 06:25 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 17
ArtandIllustration is on a distinguished road
Smile

Hello Smasher,

Sorry for later reply; have had a few net problems - Thank you very much! Seems the problem was a combination of the case sensitivity issue after all (didn't have any idea is was case aware) and I had an error in my IB linking. Thank you for your help
ArtandIllustration is offline   Reply With Quote
Reply

Bookmarks

Tags
button, compare, nsdate, string, uilabel

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: 383
6 members and 377 guests
chemistry, daudrizek, HemiMG, jeroenkeij, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek
Powered by vBadvanced CMPS v3.1.0

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