开发者

How to create a const NSDate

开发者 https://www.devze.com 2023-01-07 11:19 出处:网络
I am trying to define the equivalent of DateTime.MaxValue (C#) but in Objective C. 开发者_StackOverflow中文版I don\'t want to keep creating NSDates every time I use it so I wish I had it as const.

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

开发者_StackOverflow中文版

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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号