简体   繁体   English

如何从给定的数字中获取星期几

[英]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 ... 任何更好的解决方案...

Since this has become the accepted answer, I'll post the "right" solution here too. 由于这已成为公认的答案,因此我也将在此处发布“正确的”解决方案。 Credits to Rob's answer. 归功于Rob的回答。

The whole thing can simply be achieved using the [shortWeekdaySymbols][1] method of NSDateFormatter , so the full solution boils down to 可以使用NSDateFormatter[shortWeekdaySymbols][1]方法简单地完成整个过程,因此完整的解决方案可以归结为

- (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 . 当心,您正在将指向NSNumber的指针传递给需要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, ie NSNumber * when cast to NSInteger . 这将输出类似于185035664的值,即指针值,即185035664NSInteger NSNumber *

You should either use [dayOfWeek integerValue] or directly turn dayOfWeek into a NSInteger in your method signature. 您应该使用[dayOfWeek integerValue]或直接在方法签名dayOfWeek转换为NSInteger

Also I think you're getting something else wrong: from the doc of setWeekday: 我还认为您遇到了其他问题:来自setWeekday:的文档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. 工作日单位是从1到n的数字,其中n是一周中的天数。 For example, in the Gregorian calendar, n is 7 and Sunday is represented by 1 . 例如, 在阳历中,n是7,而星期日则由1表示

Sunday is 1, so you'd better check the correspondence with your representation too. 周日是1,所以您最好也检查一下与您的表示形式的对应关系。

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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM