如何从给定的数字中获取星期几
我想要给定数字的星期几,这是伪代码:
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];
}
我的日志很奇怪:
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 ...
既然这已成为公认的答案,我也会在这里发布正确"的解决方案.归功于 Rob 的回答.
Since this has become the accepted answer, I'll post the "right" solution here too. Credits to Rob's answer.
使用NSDateFormatter
的[shortWeekdaySymbols][1]
方法可以简单地完成整个事情,所以完整的解决方案归结为
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];
}
原答案
请注意,您正在将指向 NSNumber
的指针传递给需要 NSInteger
的方法.编译器不会警告您,因为指针确实是一个整数,只是不是您所期望的.考虑这个简单的测试:
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]
}
这会打印类似于 185035664
的内容,这是指针值,即当转换为 NSInteger
时为 NSNumber *
.
This prints something like 185035664
, which is the pointer value, i.e. NSNumber *
when cast to NSInteger
.
您应该使用 [dayOfWeek integerValue]
或直接将 dayOfWeek
转换为方法签名中的 NSInteger
.
You should either use [dayOfWeek integerValue]
or directly turn dayOfWeek
into a NSInteger
in your method signature.
此外,我认为您还有其他错误:来自 setWeekday:
Also I think you're getting something else wrong: from the doc of setWeekday:
设置接收器的工作日单位数.工作日单位是数字 1 到 n,其中 n 是一周中的天数.例如,在公历中,n 为 7,星期日为用 1 表示.
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.
星期日是 1,所以你最好也检查一下与你的代表的对应关系.
Sunday is 1, so you'd better check the correspondence with your representation too.