如何获取当前日期在Cocoa

编程入门 行业动态 更新时间:2024-10-24 22:26:44
本文介绍了如何获取当前日期在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>我得到以下错误:

警告:'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

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

发布评论

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

>www.elefans.com

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