Detect Date & Assign different function depending on day

Discussion in 'iOS Programming' started by newtoiphonesdk, Jun 24, 2011.

  1. newtoiphonesdk macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    Here is what I have so far. The only thing this needs to do is detect the date, and then depending on what that date is, post something different to Facebook. I only have it set up with one If statement for the moment just for testing. When I run it, it doesn't post the else statement, since today isn't Sunday.
    Code:
    
    -(IBAction)evite {
    	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    	[formatter setDateFormat:@"MM-dd-yyyy HH:mm"];
    	todaysDate = [formatter stringFromDate:[NSDate date]];
    	NSString *day = [todaysDate substringWithRange:NSMakeRange (3, 2)];
    	if (day = "Sunday") {
    		NSURL *url = [NSURL URLWithString:@"http://www.marsalisavenuecoc.org"];
    		SHKItem *item = [SHKItem URL:url title:@"Join me @ Marsalis Avenue Church of Christ this Sunday!  Bible Class @8:45 Worship @10:00!  Click link for our website."];
    		SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
    		[actionSheet showInView:self.tabBarController.view];
    	}
    	else
    	{NSURL *url = [NSURL URLWithString:@"http://www.marsalisavenuecoc.org"];
    	SHKItem *item = [SHKItem URL:url title:@"Join me @ Marsalis Avenue Church of Christ this Wednesday!  Bible Class @8:45 Worship @10:00!  Click link for our website."];
    	SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
    		[actionSheet showInView:self.tabBarController.view];}
    	
    	
    	
    	
    }
    
     
  2. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #2
    Thanks, if I read correctly, EEEE is what shows the full day name. I changed the formatter to this, and added the @"Sunday" to the code and have this.
    Code:
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    	[formatter setDateFormat:@"EEEE"];
    	todaysDate = [formatter stringFromDate:[NSDate date]];
    	
    	if (todaysDate = @"Sunday") {
    		NSURL *url = [NSURL URLWithString:@"http://www.marsalisavenuecoc.org"];
    		SHKItem *item = [SHKItem URL:url title:@"Join me @ Marsalis Avenue Church of Christ this Sunday!  Bible Class @8:45 Worship @10:00!  Click link for our website."];
    		SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
    		[actionSheet showInView:self.tabBarController.view];
    	}
    	else
    	{NSURL *url = [NSURL URLWithString:@"http://www.marsalisavenuecoc.org"];
    	SHKItem *item = [SHKItem URL:url title:@"Join me @ Marsalis Avenue Church of Christ this Wednesday!  Bible Class @8:45 Worship @10:00!  Click link for our website."];
    	SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
    		[actionSheet showInView:self.tabBarController.view];}
    
    The warnings disappeared, but it still doesn't give the desired effect. Am I at least heading in the correct way?
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    The first red-hilited expression is wrong in several ways:
    1. It's using assignment (=) instead of equality-test (==).
    2. It's "comparing" to a C string ("foo"), not an NSString (@"foo").
    3. Don't use == for comparing NSString equality, use the -isEqualToString: method of NSString.

    The red-hilited { } are hard to see. Even when hilited in red. Use whitespace to make them more obvious.


    For the overall design, you should probably use a switch statement, and use case labels that are the numeric values for days of the week. See NSDateComponents.
    http://developer.apple.com/library/...ptual/DatesAndTimes/Articles/dtCalendars.html
     
  4. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #4
    Got it figured out thanks to help from another developer. Code used was Gregorian calendar from NSCalendarComponent:
    Code:
    -(IBAction)evite {
    	NSCalendar* gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
    	NSDateComponents* theDateComponents = [gregorian components: NSWeekdayCalendarUnit fromDate: [NSDate date]];
    	NSInteger weekDay = theDateComponents.weekday;
    	NSLog(@"Today is day %ld", weekDay);	
    	if (weekDay == 1, 5, 6, 7) {
    		NSURL *url = [NSURL URLWithString:@"http://www.marsalisavenuecoc.org"];
    		SHKItem *item = [SHKItem URL:url title:@"Join me @ Marsalis Avenue Church of Christ this Sunday!  Bible Class @8:45 Worship @10:00!  Click link for our website."];
    		SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
    		[item setCustomValue:@"https://fbcdn-photos-a.akamaihd.net/photos-ak-snc1/v43/136/198020576909620/app_1_198020576909620_358.gif" forKey:@"picture"];
    		[actionSheet showInView:self.tabBarController.view];
    	}
    	else
    	{NSURL *url = [NSURL URLWithString:@"http://www.marsalisavenuecoc.org"];
    	SHKItem *item = [SHKItem URL:url title:@"Join me @ Marsalis Avenue Church of Christ this Wednesday!  Bible Class @10AM Study @7:00!  Click link for our website."];
    	SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
    	[item setCustomValue:@"https://fbcdn-photos-a.akamaihd.net/photos-ak-snc1/v43/136/198020576909620/app_1_198020576909620_358.gif" forKey:@"picture"];
    	[actionSheet showInView:self.tabBarController.view];}
    	
    	
    	
    	
    }
    
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    If that conditional is supposed to mean "if weekday is equal to one of the values 1, 5, 6, or 7", then that's not what it does.

    It's syntactically valid C code, which means it will compile without an error or warning. It just doesn't do what I think you intend it to do.
     
  6. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #6
    What is it doing? I run it in the simulator, and it works the way I intend. If it is Sunday Thursday Friday or Saturday it runs the code, but if it is a different day it runs the else code. However, in the actual iOS used for testing...it only uses the if statement, and never the else.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    http://en.wikipedia.org/wiki/Comma_operator

    You could use switch and case, as already mentioned.

    You could also make an NSIndexSet containing 1, 5, 6, 7, then use containsIndex:weekday in the conditional.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Or you could just "or" multiple conditionals:
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Simplified:
    Code:
    if ( weekDay == 1 || weekDay >= 5 )
    
     

Share This Page