Search code examples
iosswiftnsdate

How should my app handle the current date if it uses it throughout its runtime?


Where should I put a Date instance and how should I handle calling it in order to get the current date? Should I make a singleton with a date instance? Should I call this instance in one of AppDelegate's functions in order to update the current date when the app isn't on use?


Solution

  • The parts of your question don't really make sense:

    "Where should I put a Date instance and how should I handle calling it in order to get the current date?"

    A Date instance records a fixed moment in time. The code

    let date = Date()
    

    will record the current date at time at the instant it's called, and not change. If your program is still running tomorrow, that date will now be very "out of date".

    Instead you should use the expression Date() any time you need the current date.