Thomas Besnehard
Thomas Besnehard

Reputation: 2095

How to get the day of week from a given number

I want to have the day of week name for a given number, here is the pseudo-code :

getDayStringForInt:0 = sunday
getDayStringForInt:1 = monday
getDayStringForInt:2 = tuesday
getDayStringForInt:3 = wenesday
getDayStringForInt:4 = thursday
getDayStringForInt:5 = friday
getDayStringForInt:6 = saturday

I have tried with the follow code, but some thing is not working ...

- (void) setPeriodicityDayOfWeek:(NSNumber *)dayOfWeek{
    gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    dateFormatter = [[NSDateFormatter alloc] init];
    NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [dateFormatter setLocale:frLocale];
    [gregorian setLocale:frLocale];
    NSDate *today = [NSDate date];
    NSDateComponents *nowComponents = [gregorian components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];

    [nowComponents setWeekday:dayOfWeek];

    NSDate *alertDate = [gregorian dateFromComponents:nowComponents];

    [dateFormatter setDateFormat:@"EEEE"];
    NSLog(@"Day Of Week : %@ - Periodicity : %@", dayOfWeek, [dateFormatter stringFromDate:alertDate]);
    alert.periodicity = [dateFormatter stringFromDate:alertDate];

}

My log is very strange :

Day Of Week : 0 - Periodicity : monday
Day Of Week : 1 - Periodicity : wenesday
Day Of Week : 2 - Periodicity : friday
Day Of Week : 3 - Periodicity : friday
Day Of Week : 4 - Periodicity : tuesday
Day Of Week : 5 - Periodicity : sunday
Day Of Week : 6 - Periodicity : sunday

Any idea ? any better solution ...

Upvotes: 2

Views: 2146

Answers (3)

Gabriele Petronella
Gabriele Petronella

Reputation: 108169

Since this has become the accepted answer, I'll post the "right" solution here too. Credits to Rob's answer.

The whole thing can simply be achieved using the [shortWeekdaySymbols][1] method of NSDateFormatter, so the full solution boils down to

- (NSString *)stringFromWeekday:(NSInteger)weekday {
    NSDateFormatter * dateFormatter = [NSDateFormatter new];
    dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    return dateFormatter.shortWeekdaySymbols[weekday];
}

Original answer

Beware, you're passing a pointer to NSNumber to a method that requires a NSInteger. The compiler is not warning you since a pointer is indeed an integer, just not the one you would expect. Consider this simple test:

- (void)foo:(NSInteger)a {
    NSLog(@"%i", a);
}

- (void)yourMethod {
    [self foo:@1]; // @1 is the boxed expression for [NSNumber numberWithInt:1]
}

This prints something like 185035664, which is the pointer value, i.e. NSNumber * when cast to NSInteger.

You should either use [dayOfWeek integerValue] or directly turn dayOfWeek into a NSInteger in your method signature.

Also I think you're getting something else wrong: from the doc of setWeekday:

Sets the number of weekday units for the receiver. Weekday units are the numbers 1 through n, where n is the number of days in the week. For example, in the Gregorian calendar, n is 7 and Sunday is represented by 1.

Sunday is 1, so you'd better check the correspondence with your representation too.

Upvotes: 8

Thomas Besnehard
Thomas Besnehard

Reputation: 2095

Thanx to Every one, here is a clean response :

/**
 *  getting the day of week string for a given day of week number
 *
 *  @param  dayOfWeekNumber 0 return sunday, 6 return saturday
 *
 *  @return a string corresponding at the given day of week.
 */
- (NSString*) getDayOfWeekStringForDayOfWeek:(NSInteger)dayOfWeek{
    return [[dateFormatter shortWeekdaySymbols] objectAtIndex:dayOfWeek];
}

Upvotes: 0

Related Questions