如何获取当前日期在Cocoa [英] How do I get the current date in Cocoa
问题描述
我开始为iPhone开发,因此我在网上查看不同的教程,以及尝试一些不同的东西。目前,我想创建一个倒计时直到午夜。要获取小时,分钟和秒数,我执行以下操作(我在某处找到):
NSDate * now = [NSDate date];
int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour];
int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute];
countdownLabel.text = [NSString stringWithFormat:@%02d:%02d:%02d,hour,min,sec];但是,我使用的每个地方 -dateWithCalendarFormat:timeZone:$ c> $ c>我得到以下错误: 警告:'NSDate'可能不响应'-dateWithCalendarFormat:timeZone:'
(没有匹配方法签名的消息将被假定为返回'id'并接受'...'作为参数。)
警告:没有找到'-hourOfDay'方法
错误:无效的操作数到二进制 - (有'int'和'id')
这看起来很简单。
此外,我注意到在不同的地方,在不同的时间星号(*)位于时间 NSDate * now
或紧靠变量 NSDate * now
之前。两者有什么区别,为什么你会使用一个对另一个?
解决方案您必须使用以下方式:
code> NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents * dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit)fromDate:yourDateHere];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
[gregorian release];
现在NSDate * now和NSDate *之间没有区别,这只是一个偏好。从编译器的角度来看,没有什么改变。
I'm getting started developing for the iPhone and as such I am looking at different tutorials online as well as trying some different things out myself. Currently, I'm trying to create a countdown until midnight. To get the number of hour, minutes, and seconds, I do the following (which I found somewhere):
NSDate* now = [NSDate date];
int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour];
int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute];
countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];
However, each place I use -dateWithCalendarFormat:timeZone:
I get the following error:
warning: 'NSDate' may not respond to '-dateWithCalendarFormat:timeZone:'
(Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments.)
warning: no '-hourOfDay' method found
error: invalid operands to binary - (have 'int' and 'id')
This seems like something very simple. What am I missing?
Also, I've noticed at different places and at different times the asterisk (*) is located either right after the time NSDate* now
or right before the variable NSDate *now
. What is the difference in the two and why would you use one versus the other?
You must use the following:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:yourDateHere];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
[gregorian release];
There is no difference between NSDate* now and NSDate *now, it's just a matter of preference. From the compiler perspective, nothing changes.
这篇关于如何获取当前日期在Cocoa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!