I have a struct called DailyEvents
that initializes as a dictionary. We attempt to append to the dictionary in another class, however, receive the error:
Cannot use mutating member on immutable value because allEvents is a get-only property.
Here is the function where we try to make use of the struct and append:
DispatchQueue.main.async {
self.carePlanManager.store.enumerateEvents(of: ockActivity, startDate: self.startDate as DateComponents, endDate: self.endDate as DateComponents, handler: { (event, _) in
if let event = event {
self.dailyEvents?.allEvents.append(event)
}
}, completion: { (_, _) in
innersemaphore.signal()
})
}
Here is the definition of the struct:
struct DailyEvents {
// MARK: Properties
private var mappedEvents: [NSDateComponents: [OCKCarePlanEvent]]
var allEvents: [OCKCarePlanEvent] {
return Array(mappedEvents.values.flatMap{$0})
}
var allDays: [NSDateComponents] {
return Array(mappedEvents.keys)
}
subscript(day: NSDateComponents) -> [OCKCarePlanEvent] {
get {
if let events = mappedEvents[day] {
return events
}
else {
return []
}
}
set(newValue) {
mappedEvents[day] = newValue
}
}
// MARK: Initialization
init() {
mappedEvents = [:]
}
}
I was thinking the subscript takes care of the functionality for querying or appending to the data structure but it doesn't. Should I create an append function directly in the DailyEvents struct?