it was my previous question to simply display static data on FSCalendar using 2 arrays of present and absent days.
How to change background color of events array in
Don't use NSDictionary
in Swift, you just need to use DateFormatter
to get your formatted date. Also correct JSON
dictionary in Swift 3 is [String:Any]
.
First of all declare two instance property in your class of type [String]
named presentDateArray
and absentDateArray
.
var presentDateArray = [String]()
var absentDateArray = [String]()
Now initialize these two array in your response completion block this way.
if let responseJSON = (try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)) as? [String:Any],
let presentdetails = responseJSON["Present"] as? [[String:Any]],
let Absentdetails = responseJSON["Absent"] as? [[String:Any]] {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
self.presentDateArray = presentdetails.flatMap { dateFormatter.date(for: $0["Date"] as! String) }.flatMap { dateFormatter1.string(from:$0) }
self.absentDateArray = Absentdetails.flatMap { dateFormatter.date(for: $0["Date"] as! String) }.flatMap { dateFormatter1.string(from:$0) }
DispatchQueue.main.async {
calendar.reloadData()
}
}
Now simply use this two array in your delegate of method of
func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
let datestring2 : String = dateFormatter1.string(from:date)
if presentDateArray.contains(datestring2) {
return UIColor.green
}
else if presentDateArray.contains(datestring2) {
return UIColor.red
}
else {
return nil
}
}