John Murcielago John Murcielago - 1 year ago 149
Swift Question

NSDate Next day after midnight

I've got a very simple case, but for some reason I can't find the right way to deal with it because of my "starter" level in iDev.
User during using my app will have some limits for a one day. I set the limit in NSUserDefaults after a certain count of user actions.
For now I think it should be NSDate value, but I'm not sure.
Each time user requests some action, app has to check if there is no limit. How to check it correctly?
For example, today at 9:00 AM user went out of limit and app has to restrict any actions during midnight. The next day at 0:01 AM after checking limit it has to be "green".

A little bit confusing, I know :(

Hoa Hoa
Answer Source

To get the start of next day, you can use NSCalendar class with 2 APIs:

First: get next day of your current time (NSDate):

Returns a new NSDate object representing the absolute time calculated by adding given components to a given date.

- (NSDate *)dateByAddingComponents:(NSDateComponents *)comps
                            toDate:(NSDate *)date

your comps parameter should have only Day value = 1, like:

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];

then get the NSDate the present the first moment of your next day.

Returns the first moment date of a given date

- (NSDate *)startOfDayForDate:(NSDate *)date
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download