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

前端 未结 1 1983
隐瞒了意图╮
隐瞒了意图╮ 2020-12-20 10:24

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

相关标签:
1条回答
  • 2020-12-20 10:54

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题