I am new to swift, Am using Fscalender in swift it's working fine, But I want add the Events to Fscalender, I can get events from Json
I Want Display the events in calender, I can try some of the code but its not working getting some errors pls help how to display events in calender
var EventsData = [Event]()
all events are stored Into Event
fileprivate lazy var dateFormatter2: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
return formatter
}()
inside Json
if let event_list = jsonData["events"] as? NSArray {
for i in 0 ..< event_list.count {
if let event = event_list[i] as? NSDictionary {
let data = event["date"]as?String
let newString = data?.replacingOccurrences(of: "/", with: "-")
print("new string data ",newString as Any)
self.compareDate(date: newString!)
self.EventsData.append(Event(
eventId: event["eventId"] as? String,
eventName:event["details"] as? String,
//eventDate: event["date"] as? String
eventDate: newString ))
}
}
}
Display the Events
func compareDate(date : String){
let date = date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let dateFromString : NSDate = dateFormatter.date(from: date)! as NSDate
===>> After this line showing Fatal error
dateFormatter.dateFormat = "yyyy-MM-dd"
let datenew = dateFormatter.string(from: dateFromString as Date)
print("datee",datenew)
}
func calendar(_ calendar: FSCalendar, willDisplay cell: FSCalendarCell, for date: Date, at position: FSCalendarMonthPosition) {
let dateFormatter3 = DateFormatter()
dateFormatter3.dateFormat = "yyyy-MM"
let dateString3 = dateFormatter3.string(from: date)
//print("datenew1",dateString3)
strcond = dateString3 as NSString
print("datenew1",strcond!)
}
func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
let dateString = self.dateFormatter2.string(from: date)
for d in EventsData{
let date = d.eventDate
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let dateFromString : NSDate = dateFormatter.date(from: date!)! as NSDate
dateFormatter.dateFormat = "yyyy-MM-dd"
let datenew = dateFormatter.string(from: dateFromString as Date)
if datenew.contains(dateString) {
return 1
}
}
return 0
}
func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventColorFor date: Date) -> UIColor? {
let dateString = self.dateFormatter2.string(from: date)
for d in EventsData{
let date = d.eventDate
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let dateFromString : NSDate = dateFormatter.date(from: date!)! as NSDate
dateFormatter.dateFormat = "yyyy-MM-dd"
let datenew = dateFormatter.string(from: dateFromString as Date)
print("new calendar",dateString)
if datenew.contains(dateString) {
return UIColor.purple
}
}
return nil
}
func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
if monthPosition == .previous || monthPosition == .next {
calendar.setCurrentPage(date, animated: true)
print("title date",date)
}
}
how to show events in calendar ?
After some changes in my code it's working fine showing events in FsCalender