NSDate -dateWithNaturalLanguageString:在 iPhone 上?
在 i电话文档集中NSDate,在讨论区他们讨论了 -dateWithNaturalLanguageString:locale:
,但是他们没有在页面的其他地方记录该方法。
我之前在 iPhone 上使用过这个方法,虽然我收到了警告,但效果很好。现在我正在使用 -Werror
进行构建(我应该一直这样做^_^),我遇到了一个警告。
我将如何替换以下代码行?
NSDate *today = [NSDate dateWithNaturalLanguageString:@"today at 23:59:59"];
NSDate *tomorrow = [NSDate dateWithNaturalLanguageString:@"tomorrow at 23:59:59"];
In the iPhone docset for NSDate, in the discussion area they discuss -dateWithNaturalLanguageString:locale:
, however they don't document the method elsewhere on the page.
I've used the method before for iPhone and it worked just fine, although I got warnings. Now that I'm building with -Werror
(which I should have been doing all along ^_^) I've run into a warning with this.
How would I replace the following lines of code?
NSDate *today = [NSDate dateWithNaturalLanguageString:@"today at 23:59:59"];
NSDate *tomorrow = [NSDate dateWithNaturalLanguageString:@"tomorrow at 23:59:59"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是使用 NSCalendar 和 NSDateComponents。
要获取今天的 23:59:59:
要获取明天的 23:59:59:
One way to do this is with NSCalendar and NSDateComponents.
To get today at 23:59:59:
To get tomorrow at 23:59:59:
假设:
您可以通过以下方式获取现在和现在+24 小时:
为了获取给定日期的午夜,请务必记住
NSDate*
是免费桥接到CFDateRef,它提供了一些您想要使用的附加 API。特别是,您可以使用
CFDateGetAbsoluteTime
将NSDate
转换为CFAbsoluteTime
:...您可以将其包装到一个函数中以舍入两个
>今天
和明天
到午夜。Let's assume:
You can get now and now+24 hours with:
In order to get midnight for the given day, it's important to remember that
NSDate*
is toll-free bridged toCFDateRef
, which provides some additional APIs you'll want to use. In particular, you can convert anNSDate
to aCFAbsoluteTime
withCFDateGetAbsoluteTime
:... which you can wrap into a function to round both
today
andtomorrow
to midnight.