如何创建一个常量 NSDate

发布于 2024-09-10 04:20:54 字数 450 浏览 1 评论 0原文

我正在尝试定义 DateTime.MaxValue (C#) 的等效项,但在 Objective C 中。

我不想每次使用它时都继续创建 NSDates,所以我希望将其作为 const。

问题,编译器返回“Initializer element is not Constant”

下面是代码

static NSDate* DateTimeMinValue = [NSDateFormatter dateFromString:@"00:00:00.0000000, January 01, 1984"];

我还尝试了

NSDate* const DateTimeMinValue = [NSDateFormatter dateFromString:@"00:00:00.0000000, 1984 年 1 月 1 日"];

另外,static 和 const 之间有什么区别?

I am trying to define the equivalent of DateTime.MaxValue (C#) but in Objective C.

I don't want to keep creating NSDates every time I use it so I wish I had it as const.

The problem, the compiler returns "Initializer element is not constant"

Here is the code

static NSDate* DateTimeMinValue = [NSDateFormatter dateFromString:@"00:00:00.0000000, January 01, 1984"];

I also tried

NSDate* const DateTimeMinValue = [NSDateFormatter dateFromString:@"00:00:00.0000000, January 01, 1984"];

Also, what would the difference between the static and const be?

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

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

发布评论

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

评论(2

心舞飞扬 2024-09-17 04:20:54

虽然这不是您问题的确切答案,但它仍然可能对您有所帮助。

您是否读过或听说过

[NSDate distantPast];
[NSDate distantFuture];

他们回来的

消息

一个 NSDate 对象,表示遥远未来的日期(以世纪为单位)。

当您必须将日期与遥远的日期进行比较时,它们会非常方便。

While this is not the exact answer to your question it still might help you.

Did you ever read or hear about

[NSDate distantPast];
[NSDate distantFuture];

they return

An NSDate object representing a date in the distant future (in terms of centuries).

They come in quite handy when you have to compare dates to well a distant Date.

吾家有女初长成 2024-09-17 04:20:54

static 和 const 之间的区别是什么?

静态变量在应用程序运行会话的生命周期中存在,并且可以从类内部或其他类通过访问器来访问和更改。 const 变量不能被修改。

what would the difference between the static and const be?

Static variables exist for the life of the app's run session, and can be accessed and altered from within the class, or by other classes by way of accessors. Const variables cannot be modified.

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