In the app I am currently working on, there is a part where user can set multiple local notifications for specific weekdays.

enter image description here

The array of buttons is represented by a separate class, which utilises stack view. When any of the buttons is clicked, its index is stored in an array, and with this array I can properly display selected buttons later on.

The class that is responsible for storing information about notifications is called Reminder, and have the following properties:

let reminderId: UUID
var reminderMessage: String
var dateComponents: DateComponents
var daysOfWeek = [Int]()

Property daysOfWeek is responsible for storing information about on which days of week to fire a notification. However, in reality it stores indices of selected buttons representing days of week. So for 7 buttons representing weekdays, this property can have multiple values from 0 to 6 accordingly. Hence, when loading saved reminders from memory and showing them in table view, I use this information to correctly display days of week button array with selected days.

As we know, the DateComponents class has a .weekday property which is pretty self explanatory. Each week day has its own index, which can be shown as:

let weekDaysDict = [1: "Sunday", 2: "Monday", 3: "Tuesday", 4: "Wednesday", 5: "Thursday", 6: "Friday", 7: "Saturday"]

However, in my Reminder class the same properties would be represented as such:

let originalDays = [0: "Monday", 1: "Tuesday", 2: "Wednesday", 3: "Thursday", 4: "Friday", 5: "Saturday", 6: "Sunday",]

Hence, in the separate class called NotificationsManager, for scheduling and unscheduling local notifications I have to transform the indices stored in the Reminder class' daysOfWeek: [Int] property to appropriately represent weekdays. As a result, I use a simple function:

private static func shiftNumbers(array: [Int]) -> [Int] {
    var newArray = [Int]()
    for i in 0..<array.count {
        if array[i] == 6 {
        else {
            newArray.append(array[i] + 2)
    return newArray

With all this information this is how my method for scheduling multiple notifications looks:

static func scheduleMultipleNotifications(reminder: Reminder) {
    let daysToSchedule = shiftNumbers(array: reminder.daysOfWeek)   
    for i in 0 ..< daysToSchedule.count {
        let id = "\(reminder.reminderId)_\(weekDaysDict[daysToSchedule[i]]!)"
        let message = reminder.reminderMessage
        let components = reminder.dateComponents
        let weekDay = daysToSchedule[i]            

        scheduleNotification(notificationRequestId: id, notificationMessage: message, dateComponents: components, weekDay: weekDay)

I believe it should be the responsibility of Reminder class to store correctly converted indices of weekdays, and that the NotificationsManager class should only be responsible for scheduling notifications and not transforming one array of indices into another.

Another problem is that my Reminder class in its current form, only stores a single reminder id, and later on for scheduling multiple notifications, since UNNotificationRequest requires unique identifier for each new request, I have to create several ids as shown in the code snippet from above method:

for i in 0 ..< daysToSchedule.count {
        let id = "\(reminder.reminderId)_\(weekDaysDict[daysToSchedule[i]]!)"

Where weekDaysDict is a dictionary representing names of weekdays and their indices. So a single id will look something like this: 0A3276DE-FEDF-4BC6-BA79-2DCC374D4BA9_Thursday - consists of UUID of reminder class and added weekday name at the end. The same array of ids must be created when deleting multiple notifications. I was thinking maybe I should use an array of ids in the Reminder class, rather than single id, where each id will be responsible for each weekday.

Based on all of the information provided how would you guys suggest I design my Reminder class, so that it is more cohesive?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.