Missing argument for parameter 'referenceDate' in call in swift

My COUNTDOWNApp.swift code
where to put the date
please help and give exact instructions as i am a noob
I need to make a count down to an event

import SwiftUI

@main
struct COUNTDOWNApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

and my ContentView.swift

import SwiftUI

struct ContentView : View {
    
    @State var nowDate: Date = Date()
    let referenceDate: Date
    var timer: Timer {
        Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {_ in
            self.nowDate = Date()
        }
    }
    
    var body: some View {
        Text(countDownString(from: referenceDate))
            .font(.largeTitle)
            .onAppear(perform: {
                _ = self.timer
            })
    }

    func countDownString(from date: Date) -> String {
        let calendar = Calendar(identifier: .gregorian)
        let components = calendar
            .dateComponents([.day, .hour, .minute, .second],
                            from: nowDate,
                            to: referenceDate)
        return String(format: "%02dd:%02dh:%02dm:%02ds",
                      components.day ?? 00,
                      components.hour ?? 00,
                      components.minute ?? 00,
                      components.second ?? 00)
    }

}

Does this kind of work for you?

struct ContentView : View {

    @State private var nowDate: Date = Date()
    @State private var referenceDate: Date = Date()
    var timer: Timer {
            Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {_ in
                self.nowDate = Date()
            }
        }

    var body: some View {
        Text(countDownString(from: referenceDate))
            .font(.largeTitle)
            .onAppear(perform: {
                getReferenceDate()
                _ = self.timer
            })
    }

    func countDownString(from date: Date) -> String {
        let calendar = Calendar(identifier: .gregorian)
        let components = calendar
            .dateComponents([.day, .hour, .minute, .second],
                            from: nowDate, to: referenceDate
                            )
        return String(format: "%02dd:%02dh:%02dm:%02ds",
                      components.day ?? 00,
                      components.hour ?? 00,
                      components.minute ?? 00,
                      components.second ?? 00)
    }

    func getReferenceDate() {
        let calendar = Calendar.current
        let date = calendar.date(byAdding: .minute, value: 2, to: Date())
        referenceDate = date!
    }

}

Essentially what I have done is generate a reference date to countdown to by calling the getReferenceDate() function in .onAppear. That date is 2 minutes from the current Date.

Let me know it that’s what you are looking for.

lots of errors in this code

What errors are you getting?

What version of Xcode are you using?

Version 12.5.1 (12E507)

Exactly the same as I have and the code works perfectly.

please send me the file

on we transfer

the date i need to count down to 27 july 2021

There should be no need for me to send you the project. The ContentView code I pasted in my reply should work for you. Either overwrite your code with this… or create a new test project and paste this into that project in place of the ContentView in that new project.

Just for exercise I adjusted the referenceDate calculation to add 24 days to the current date which would be July 27.

struct ContentView : View {

    @State private var nowDate: Date = Date()
    @State private var referenceDate: Date = Date()
    var timer: Timer {
            Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {_ in
                self.nowDate = Date()
            }
        }

    var body: some View {
        Text(countDownString(from: referenceDate))
            .font(.largeTitle)
            .onAppear(perform: {
                getReferenceDate()
                _ = self.timer
            })
    }

    func countDownString(from date: Date) -> String {
        let calendar = Calendar(identifier: .gregorian)
        let components = calendar
            .dateComponents([.day, .hour, .minute, .second],
                            from: nowDate, to: referenceDate)
        return String(format: "%02dd:%02dh:%02dm:%02ds",
                      components.day ?? 00,
                      components.hour ?? 00,
                      components.minute ?? 00,
                      components.second ?? 00)
    }

    func getReferenceDate() {
        let calendar = Calendar.current
        let date = calendar.date(byAdding: .day, value: 24, to: Date())
        referenceDate = date!
    }

}

errors

soory i had not put import swiftui

Ah yeah that would help.

If you specifically want to have the countdown to 27-July-2021 then the following adjustment to getReferenceDate() will work for you.

    func getReferenceDate() {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd-MM-yyyy"
        let newDate = "27-07-2021"
        let date = dateFormatter.date(from: newDate)
        referenceDate = date!
    }
1 Like

A better way of doing this is as follows:

let referenceDate: Date = {
        let future = DateComponents(
            year: 2021,
            month: 7,
            day: 27,
            hour: 8
        )
        return Calendar.current.date(from: future)!
    }()

and you can remove the getReferenceDate() function.

The future date is 27, July, 2021 at 8am in the morning.