How to make API call for this web service to fetch array of present and absent dates separately in swift?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 13:11:51

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
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!