-1

我正在制作我的第一个应用程序,这是我最大的问题。我想发出一个通知,在其中我可以根据用户输入向它添加操作。我首先创建了一个没有操作的类别,然后使用 for 循环我尝试创建操作并同时将其添加到类别中,但这里显示的问题是:无法分配给属性:“操作”是仅获取属性

这是我的代码:

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
            }

我该如何解决我的问题,还有其他方法吗?我希望动作数组是可变的,所以我以后也可以从中删除一些动作。

任何帮助将不胜感激,这对我来说意味着梦想成真!

4

1 回答 1

1

如错误所示,UNNotificationCategory.actions数组是只读的,可以通过 UNNotificationCategory构造方法修改,所以试试这种方式一定对你有用

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: [])
于 2017-11-16T16:24:12.947 回答