-1

I am making my first app and here is my biggest problem. I would like to make a notification in which i can add actions to it based of user input. I have made a category with no actions at first and then with a for loop I try to create actions and at the same time add it to the category but here is the problem where it shows: Cannot assign to property: 'actions' is a get-only property

Here is my code:

var category5 = UNNotificationCategory(identifier: "category.5", actions: [], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: "placeholdertext", options: [])

    for index in 0..<workingDays[3].subjects!.count {
         let action = UNNotificationAction(identifier: "\(workingDays[3].subjects![index + 1].subjectName).5", title: workingDays[3].subjects![index + 1].subjectName, options: .foreground)

                category5.actions += [action]  // here I get the error
                // category5.actions.append(action) // I tried this as well but I get the same error
            }

How can i fix my problem and is there another way of doing this? I would like the actions array to be mutable so I can also later remove some actions from it.

Any help would be much much appreciated and It for me that would mean dream come true !

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

1

As the error shows, the UNNotificationCategory.actions array is read only and can be modified through the UNNotificationCategory constructor method, so try this way must work for you

var actionsToAdd : [UNNotificationAction] = []
    for index in 0..<workingDays[3].subjects!.count {
         let action = UNNotificationAction(identifier: "\(workingDays[3].subjects![index + 1].subjectName).5", title: workingDays[3].subjects![index + 1].subjectName, options: .foreground)

                actionsToAdd.append(action)
            }

var category5 = UNNotificationCategory(identifier: "category.5", actions: actionsToAdd, intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: "placeholdertext", options: [])
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55