Showing a welcome screen once per day I have this method, which first basically shows a Welcome screen to user only once per day. The code I have so far is this

private func showGreetingScreen (tabbar: UITabBarController) {
    let defaults = UserDefaults.standard
    if defaults.object(forKey: "greetingDate") == nil {
        defaults.set(Date(), forKey: "greetingDate")
        presentWelcomeScreen(tabbar: tabbar)
    } else {
        if let greetingDate = defaults.object(forKey: "greetingDate") as? Date {
            let order = Calendar.current.compare(Date(), to: greetingDate, toGranularity: .day)
            if order == .orderedDescending {
                defaults.set(Date(), forKey: "greetingDate")
                presentWelcomeScreen(tabbar: tabbar)
            }
        }
    }
}


Now as you can see first I am checking whether Welcome Screen was shown in past or not and then I am saving current date. Then next time app will load, it will compare current date and the date Welcome Screen was shown and based on that It will show Welcome screen to User.
I have feeling that this code can be refactored more.
There is some code duplication for the cases “greeting date has never been set” and “last greeting was yesterday or earlier.” This can be avoided if you use the nil-coalescing operator ?? to set the last greeting date to a default value in the past:

let defaults = UserDefaults.standard
let lastGreeting = defaults.object(forKey: "greetingDate") as? Date ?? .distantPast
let now = Date()
if Calendar.current.compare(now, to: lastGreeting, toGranularity: .day) == .orderedDescending {
    defaults.set(now, forKey: "greetingDate")
    presentWelcomeScreen(tabbar: tabbar)
}