0

I'm having an 2arrays of dates and want to set different colors for particular event please help how to do this.

I'm trying to implement following code but its not working its returning nil no color effects no error nothing m stucked on this please help.

import UIKit
import FSCalendar

class myCalendarViewController: UIViewController,FSCalendarDelegate,FSCalendarDataSource,FSCalendarDelegateAppearance {

    var presentdays = [String]()
    var absentdays = [String]()

@IBOutlet weak var calendar: FSCalendar!

fileprivate let gregorian: Calendar = Calendar(identifier: .gregorian)
fileprivate lazy var dateFormatter1: DateFormatter = {
   let formatter = DateFormatter()
   formatter.dateFormat = "yyyy/MM/dd"
   return formatter
}()

override func viewDidLoad() {
      super.viewDidLoad()
}

func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance,  titleDefaultColorFor date: Date) -> UIColor? {
         presentdays = ["2017-06-03",
                        "2017-06-06",
                        "2017-06-12",
                        "2017-06-25"]

         absentdays = ["2017-06-10",
                       "2017-06-18",
                       "2017-06-15",
                       "2017-06-16"]

let datestring2 : String = dateFormatter1.string(from:date)

if presentdays.contains(datestring2)
{
    return UIColor.green
}
else if absentdays.contains(datestring2)
{
    return UIColor.red
}
else{
    return nil
}

}
   }
BugFinder
  • 147
  • 1
  • 9

1 Answers1

0

Your dateFormat of DateFormatter and string date in array doesn't match so changes the dateFormat to yyyy-MM-dd and then check array contains object or not.

fileprivate lazy var dateFormatter1: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    return formatter
}()
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • @BugFinder Welcome mate :) – Nirav D Jun 16 '17 at 05:59
  • hey @Nirav can you guide me on how to make api call of my above question's web service ? below is my API link http://ezschoolportalapi.azurewebsites.net/api/Student/AttendanceDetails?schoolid=1&studentid=2&month=6&year=2017 will you please guide me ? – BugFinder Jun 16 '17 at 11:46
  • check this out https://stackoverflow.com/questions/44588656/how-to-make-api-call-for-this-web-service-to-fetch-array-of-present-and-absent-d. :) – BugFinder Jun 16 '17 at 11:55
  • but I can't figure out dude how to make it array of date objects from this JSON Response :( – BugFinder Jun 16 '17 at 12:01
  • @BugFinder You need to show the code of how you are making API call i'm not talking about making array of dates. – Nirav D Jun 16 '17 at 12:02
  • hey mate will you help me to solve this question https://stackoverflow.com/questions/44670289/how-to-make-http-post-request-with-specific-body-and-also-want-to-access-token – BugFinder Jun 21 '17 at 08:15