In my app I want to display notifications when the app startups that states the number of events of the current day. So far my code looks like this:
//after appending the arrays with data from api
DispatchQueue.main.async {
print("Dates in array: \(self.datesArray)")
if self.datesArray.contains(dateString){
//check how many events on that day
let newIndex = self.datesArray.index(of: dateString)
print("newIndex: ", newIndex!)// position of current date
print("Events today: ", self.datesArray)
self.notificationPublisher.sendNotification(title: "Events", subtitle: "", body: "\(self.datesArray.count) today", delayInterval: nil )
} else {
self.notificationPublisher.sendNotification(title: "Events", subtitle: "", body: "No events today", delayInterval: nil)
}
}
I thought about somehow finding a way to have the datesArray
array fill up so I can display the number of events of the current day but I'm unsure of how to do that. Am I going about this in the right way?
EDIT: I used a method of checking repeating elements in an array from here: How to count occurrences of an element in a Swift array?
So now my code looks like this: `DispatchQueue.main.async { print("Dates in array: (self.datesArray)")
if self.datesArray.contains(dateString){
self.datesArray.forEach{self.counts[$0, default: 0] += 1}
print("Counts: ", self.counts)
self.notificationPublisher.sendNotification(title: "Events", subtitle: "", body: "No events today", delayInterval: nil)
}
}`
Count outputs all the array elements and how many occurrences of them there are. How can I use that data to display how many events are on a particular day?