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

编程入门 行业动态 更新时间:2024-10-28 16:27:06
本文介绍了如何从给定的数字中获取星期几的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想要给定数字的星期几,这是伪代码:

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.

更多推荐

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

本文发布于:2023-10-18 06:12:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1503360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:星期   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!