检查 NSDate 是否包含日期或为空

发布于 2024-08-24 17:09:37 字数 79 浏览 3 评论 0原文

查看 NSDate 是否实际设置为日期或为空的最佳方法是什么?返回其描述或将其更改为字符串会返回“(null)”...?

谢谢。

What would be the best way to see if an NSDate is actually set to a date or is empty? Returning its description or changing it to string returns "(null)"...?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云淡月浅 2024-08-31 17:09:37

如果您想查看 NSDate 的实例(或任何对象)是否为 nil,只需将其与 nil 进行比较即可。非空的 NSDate 对象永远不会为空;它总是包含一个日期。 -init+date 都返回一个初始化为当前日期和时间的 NSDate 对象,并且没有其他方法可以创建它的实例这堂课。

   if(someData == nil) {
      // do stuff
   }

If you want to see if an instance of NSDate (or any object) is nil, just compare it to nil. A non-null NSDate object will never be empty; it always contains a date. Both -init and +date return an NSDate object initialized to the current date and time, and there is no other way to create an instance of this class.

   if(someData == nil) {
      // do stuff
   }
濫情▎り 2024-08-31 17:09:37

如果它不包含日期,那么你有一个 nil 指针,所以简单

if (!date) {
    ...
}

或更明确

if (date == nil) {
    ...
}

if it doesn't hold a date, then you have a nil pointer, so simply

if (!date) {
    ...
}

or more explicitly

if (date == nil) {
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文