2

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 ...

Thomas Besnehard
  • 2,106
  • 3
  • 25
  • 47
  • 3
    There is a much easier way to get the weekday names. See `NSDateFormatter weekdaySymbols`. – rmaddy Aug 19 '13 at 15:43
  • Your locale is incorrect. If you want US English it needs to be "en_US". The language code (in lowercase) comes first, then the country code (in uppercase). – rmaddy Aug 19 '13 at 15:45
  • right thanx but the big problems had been found by Gabriele Petronella – Thomas Besnehard Aug 19 '13 at 16:19
  • 1
    Yes, I know. But your code can be reduced to 3 lines of code if you simply use `weekdaySymbols`. There is no need for `NSCalendar`, `NSDateComponents`, or `NSDate`. All you need is a simple `NSDateFormatter` and then access the `weekdaySymbols` array to directly get the name of the weekday from your index. – rmaddy Aug 19 '13 at 16:22
  • @rmaddy is absolutely right, it's a shame that the accepted answer would not feature the "correct" solution, so here it is. – Gabriele Petronella Aug 19 '13 at 17:58
  • @rmaddy and gabriele-petronella you are right. rmaddy I've change my answer to fit your solution, I want to give you the correct answer marker, but you need to answer my question for that, and not only comment it. You can copy/past my answer, and i will make it "correct answer" - I don't was to be the correct answer, that's not fair – Thomas Besnehard Aug 19 '13 at 18:34

3 Answers3

8

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.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • Petronella, thank you very much an accurate, complete, and very well explain answer ! I didn't know what was boxed expression, I'll google that right away. I feel a bit stupid by using NSNumber so poorly, I'm not very good at understanding what it the point of having this class NSNumber ... – Thomas Besnehard Aug 19 '13 at 15:06
  • You're welcome. Here's a relevant answer about `NSNumber`: http://stackoverflow.com/questions/1285098/whats-the-difference-between-nsnumber-and-nsinteger and also http://stackoverflow.com/questions/6662730/nsnumber-vs-int – Gabriele Petronella Aug 19 '13 at 15:11
0

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];
}
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
Thomas Besnehard
  • 2,106
  • 3
  • 25
  • 47