mv79
mv79

Reputation: 35

If statement not being called

I'm Simply trying to change the text on a UILabel on a Friday between 4 and 6pm using the below code but the if statement is not being called? I am using a subclass of UILabel via Interface Builder.

- (void)viewDidLoad {
    [super viewDidLoad];
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self     selector:@selector(targetMethod) userInfo:nil repeats:YES];
}

-(void)targetMethod {
    NSLog(@"Called Timer");

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"EEE hh mm"];
    NSDate *dt = [NSDate date];
    NSString *dayAsString = [formatter stringFromDate:dt];
    NSArray *array = [dayAsString componentsSeparatedByString: @""];
    if([[array objectAtIndex:0] isEqualToString:@"Friday"] && [[array     objectAtIndex:1] intValue]>=4 &&  [[array objectAtIndex:1] intValue]<=6 &&   [[array objectAtIndex:2] intValue]>=0 && [[array objectAtIndex:2] intValue]  <=59)
    {
        self.scrollingLabel.text = @"On Air";
        NSLog(@"On Air");
    }
    else 
    {
        self.scrollingLabel.text = @"Off Air";
        NSLog(@"Off Air");
    }
}

Any help on this would be very much appreciated.

Upvotes: 2

Views: 88

Answers (4)

isaced
isaced

Reputation: 1804

Do you want to split time with spaces string it?

NSArray *array = [dayAsString componentsSeparatedByString:@" "];

And "EEE" will return "Fri", not "Friday".


Also, if you need to manipulate date/time you can look at this library : DateTools

#import "DateTools.h"

NSDate *date = [NSDate date];
if (date.weekday == 6 && date.hour >= 16 && date.hour <= 18) {
    NSLog(@"On Air");
}else{
    NSLog(@"Off Air");
}

Upvotes: 3

Sujay
Sujay

Reputation: 2520

You have made 2 mistakes

  1. [formatter setDateFormat:@"EEE hh mm"]; in this "EEE" will show only 3 characters of day like "Fri".
  2. NSArray *array = [dayAsString componentsSeparatedByString: @""]; will return only one object in array.

Now you need to do

  1. Either change "Friday" to "Fri" in you if condition or use "EEEE" (4E) that will return full name like "Friday".
  2. Separate by single space in NSArray *array = [dayAsString componentsSeparatedByString: @" "];

Upvotes: 2

Nishanth Salinamakki
Nishanth Salinamakki

Reputation: 23

Technically, your components are not being separated into individual elements at all. You forgot the space in the quotes after the 'componentsSeparatedByString' method. It should be:

NSArray *array = [dayAsString componentsSeparatedByString: @" "]; 

I tested it out, and the if statement should be called and return the specified output. Also, the first element returns only the first three characters of the day, such as 'Tue' for Tuesday. Change Friday to 'Fri' and you should be good.

Upvotes: 0

ramana_k
ramana_k

Reputation: 1933

setDateFormat:@"EEE hh mm"

With 3 E's in the date format, it returns short version of Weekday, like "Fri".

I think you need to change the comparison from "Friday" to "Fri"

Upvotes: 1

Related Questions