Hello everyone,
I have an app that lets users create and save meeting information. They can enter a date and set a repeat (like Apple's calendar app) if the meeting repeats every week, every month, every day, etc.
I'm storing these dates in CoreData using a date attribute. My questions are:
I'm thinking this will probably involve a few if statements (e.g. if repeats == 1, set this rule, else if repeats == 2, set this rule, etc.)
But what would this rule look like?
I have an app that lets users create and save meeting information. They can enter a date and set a repeat (like Apple's calendar app) if the meeting repeats every week, every month, every day, etc.
I'm storing these dates in CoreData using a date attribute. My questions are:
Can I store an array of dates in one date attribute in CoreData?
How should I create a repeating rule for the dates? Right now, I let users choose a repeat by using a picker:
Code Block Picker("Repeats", selection: $repeats) { Text("Every week").tag("1") Text("Every month").tag("2") Text("Every day").tag("3") }
I'm thinking this will probably involve a few if statements (e.g. if repeats == 1, set this rule, else if repeats == 2, set this rule, etc.)
But what would this rule look like?
Well, I'll make extra effort to help, but it would be much better for you to try, fail, retry and get it.You would learn much more.
I did not test the code below, so there may be some points to correct. In addition, is not specifically written for SwiftUI syntax.
Now, it is up to you to fine tune this. Wish you best.
And don't forget to close the thread.
I did not test the code below, so there may be some points to correct. In addition, is not specifically written for SwiftUI syntax.
Code Block // get startDate let repeats = 2 let startDate = Date() // Change with what you get from the picker // get endDate let endDate = Date() // Change with the second date you get from picker var repeatArray : [Date] = [] var runningDate = startDate let calendar = Calendar.current let dateComponents = calendar.dateComponents( [.calendar, .timeZone, .year, .month, .day], from: runningDate) // The repeat loop while runningDate <= endDate { repeatArray.append(runningDate) // add increment to runningDate switch repeats { case 1 : runningDate = calendar.date(byAdding: .day, value: 1, to: runningDate)! // ?? endDate case 2 : runningDate = calendar.date(byAdding: .day, value: 7, to: runningDate)! case 3 : runningDate = calendar.date(byAdding: .month, value: 1, to: runningDate)! // you increment the month default: break } }
Now, it is up to you to fine tune this. Wish you best.
And don't forget to close the thread.