How to show event dots in fs calendar from an array

3.1k views Asked by At

I'm using fs calendar and i'm trying to set event dots from an array named dates. this array has event dates in it that are saved in string form. so i have to convert each index to date and then set an event dot for that date. here is my attempt to do so:

if dates.isEmpty == false {
    func calendar(_calendar: FSCalendar!, hasEventForDate dateFormatter: DateFormatter) -> Bool {
    for i in 0...dates.count - 1 {
        let dateFormatter = DateFormatter ()
        dateFormatter.dateFormat = "yyyy/MM/dd"
        dateFormatter.locale = Locale.init(identifier: "fa_IR")

        dateFormatter.date(from: dates[i])

        dateFormatter.dateFormat = "yyyy/MM/dd"

        return true
    }
    return false

}
}

but nothing happens and there is no event dot when i compile the code. what am i doing wrong?

1

There are 1 answers

0
Reinier Melian On BEST ANSWER

First recommendation, use a dictionary instead of array of dates, is less demanding to lookup dates in a dictionary

Second if you need use values in an array then you should use for in and use objects directly, not index to get objects in the original array, also declare the date formatter only once

Third you need use func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int because func calendar(_calendar: FSCalendar!, hasEventForDate dateFormatter: DateFormatter) -> Bool is deprecated

Try with this code

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy/MM/dd"
    dateFormatter.locale = Locale.init(identifier: "fa_IR")

    for dateStr in dates{
        if(dateFormatter.string(from: date) == dateStr)
        {
            return 1
        }
    }
    return 0
}