Can't Figure Out How to Use Function

Hello everyone,

I'm trying to use the

Code Block
func isDateInToday(_ date: Date) -> Bool


function in my code. I've put it where functions usually go, but it expects me to return something. Basically, I don't know what to put

Code Block
func isDateInToday(_ date: Date) -> Bool {
// here
}

I might be really confusing how this function should be used. By the way, I'm calling this function like this:

Code Block
if isDateInToday(oneTimeDate) {
    Text("Today at \(oneTimeDate, formatter: itemFormatter)")
}

Answered by OOPer in 663068022

Any thoughts?

Assume you want to call this isDateInToday(_:):

isDateInToday(_:)

(When you ask something about functions defined in a framework, you should better include a link to the doc of it.)

In this case, isDateInToday is an instance method. When you want to call an instance method of some other type, you need to prefix instanceName:
Code Block
instanceName.isDateInToday(oneTimeDate)


The instance needs to be of type Calendar, you can get the instance of the user's current Calendar with Calendar.current:
Code Block
struct OneTimeRow: View {
let calendar = Calendar.current //<- An instance of `Calendar`
//...
    var body: some View {
//...
if calendar.isDateInToday(oneTimeDate) {
Text("Today at \(oneTimeDate, formatter: itemFormatter)")
} else if calendar.isDateInYesterday(oneTimeDate) { //<- `isDateInYesterday`, not `isDateinYesterday`
Text("Yesterday at \(oneTimeDate, formatter: itemFormatter)")
} else if calendar.isDateInTomorrow(oneTimeDate) {
Text("Tomorrow at \(oneTimeDate, formatter: itemFormatter)")
} else {
//...
}
//...
}
}


The isDateInToday function is part of the Foundation framework. You should be calling that function (like in your last code snippet) and not writing your own version of the function.

If you are getting a compiler error about a missing isDateInToday function, import the Foundation framework in your Swift code.

Code Block
import Foundation

Ok, I've imported Foundation, but I'm still getting a compiler error: "Cannot find 'isDateInToday' in scope". I've cleaned the build folder and attempted to build.

My code:

Code Block
if isDateInToday(oneTimeDate) {
                    Text("Today at \(oneTimeDate, formatter: itemFormatter)")


oneTimeDate is a variable of type date.
Are you sure the Text statement is correct? Does the code compile if you provide a static string, such as the following:

Code Block
Text("Today")

If the Text statement is correct and you still get a compiler error, you are going to have to supply more code for anyone to help you. Show the code for the SwiftUI view.
Text statement is correct - doesn't compile even with static string. Code is below:

Code Block
import SwiftUI
import Foundation
struct OneTimeRow: View {
    @Environment(\.openURL) var openURL
    var oneTimeName: String
    var oneTimeLink: String?
    var oneTimeDate: Date
    var oneTimeNotes: String?
    var body: some View {
        HStack {
            VStack(alignment: .leading){
                Text("\(oneTimeName)")
                    .font(.title3)
                    .fontWeight(.semibold)
                    .padding(.top, 5.0)
                    .padding(.bottom, 0)
                if isDateInToday(oneTimeDate) {
                    Text("Today at \(oneTimeDate, formatter: itemFormatter)")
                } else if isDateinYesterday(oneTimeDate){
                    Text("Yesterday at \(oneTimeDate, formatter: itemFormatter)")
                } else if isDateInTomorrow(oneTimeDate) {
                    Text("Tomorrow at \(oneTimeDate, formatter: itemFormatter)")
                } else {
                    Text("\(oneTimeDate, formatter: showDayFormatter) at \(oneTimeDate, formatter: itemFormatter)")
                        .foregroundColor(Color.gray)
                        .padding(.bottom, 3.0)
                        .padding(.top, -3)
                }
            }
            Spacer()
            Button {
                print("Sending to computer")
                guard let data = URL(string: "\(oneTimeLink ?? "undefined")") else { return }
                let av = UIActivityViewController(activityItems: [data], applicationActivities: nil)
                UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)
            } label: {
                Image(systemName: "wave.3.right.circle.fill")
                    .font(.title)
                    .padding(.trailing, 1)
            }
            .buttonStyle(BorderlessButtonStyle())
            Button {
                print("Joining on iPhone")
                openURL(URL(string: "\(oneTimeLink ?? "untitled")")!)
            } label: {
                Text("JOIN")
                    .fontWeight(.semibold)
                    .foregroundColor(Color.white)
                    .frame(width: 60.0, height: 30.0)
                    .padding(.trailing, 1.0)
                    .background(/*@START_MENU_TOKEN@*//*@PLACEHOLDER=View@*/Color.blue/*@END_MENU_TOKEN@*/)
                    .cornerRadius(20.0)
            }
            .buttonStyle(BorderlessButtonStyle())
            NavigationLink(destination: MeetingsDetailView(detailMeetingName: "\(oneTimeName)", detailMeetingLink: "\(oneTimeLink ?? "Undefined")",  detailMeetingTime: oneTimeDate, detailMeetingNotes: "\(oneTimeNotes ?? "No notes")")) {
            }
            .frame(width: 10)
        }
    }
}
private let itemFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.timeStyle = .short
    return formatter
}()
private let showDayFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateStyle = .short
    return formatter
}()

Any thoughts?
Accepted Answer

Any thoughts?

Assume you want to call this isDateInToday(_:):

isDateInToday(_:)

(When you ask something about functions defined in a framework, you should better include a link to the doc of it.)

In this case, isDateInToday is an instance method. When you want to call an instance method of some other type, you need to prefix instanceName:
Code Block
instanceName.isDateInToday(oneTimeDate)


The instance needs to be of type Calendar, you can get the instance of the user's current Calendar with Calendar.current:
Code Block
struct OneTimeRow: View {
let calendar = Calendar.current //<- An instance of `Calendar`
//...
    var body: some View {
//...
if calendar.isDateInToday(oneTimeDate) {
Text("Today at \(oneTimeDate, formatter: itemFormatter)")
} else if calendar.isDateInYesterday(oneTimeDate) { //<- `isDateInYesterday`, not `isDateinYesterday`
Text("Yesterday at \(oneTimeDate, formatter: itemFormatter)")
} else if calendar.isDateInTomorrow(oneTimeDate) {
Text("Tomorrow at \(oneTimeDate, formatter: itemFormatter)")
} else {
//...
}
//...
}
}


Can't Figure Out How to Use Function
 
 
Q