1

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?

oyvindhauge
  • 3,496
  • 2
  • 29
  • 45
HamdyAli
  • 173
  • 3
  • 14

1 Answers1

1

Check if your self.datesArray have some values with,

print(self.datesArray)

And calculate the number of today's events as:

let todayEvent = self.datesArray.filter({ $0 == dateString })

In total:

let datesArray = ["2018-08-21", "2018-08-10", "2018-08-30", "2018-09-17", "2018-08-21", "2018-09-17"]

fileprivate lazy var dateFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    return formatter
}()

func postNotificationForTodaysEvent() {
    print("Dates in array: \(self.datesArray)")

    DispatchQueue.main.async {
        let dateString = self.dateFormatter.string(from: Date())
        let todayEvent = self.datesArray.filter({ $0 == dateString }) // It will fetched the event from same date
        print(todayEvent)
        if todayEvent.isEmpty {
            self.notificationPublisher.sendNotification(title: "Events", subtitle: "WeAsean", body: "No events today", delayInterval: nil)
        } else {
            self.notificationPublisher.sendNotification(title: "Events", subtitle: "WeAsean", body: "\(todayEvent.count) today", delayInterval: nil )
        }
    }
}

and, result will be:

Dates in array: ["2018-08-21", "2018-08-10", "2018-08-30", "2018-09-17", "2018-08-21", "2018-09-17"]

today's events are:["2018-09-17", "2018-09-17"]
Ankit Jayaswal
  • 5,549
  • 3
  • 16
  • 36