FSCalender 事件显示在 swift

FSCalender Events Show in swift

我是 swift 的新手,我在 swift 中使用 Fscalender 它工作正常,但我想将事件添加到 Fscalender,我可以从 Json

我想在日历中显示事件,我可以尝试一些代码,但它不起作用出现一些错误请帮助如何在日历中显示事件

   var EventsData = [Event]()

  all events are stored Into Event


   fileprivate lazy var dateFormatter2: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"



        return formatter
    }()

里面 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 ))
                                    }


                                }
 }

显示事件

         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)
                }
            }

如何在日历中显示事件?

对我的代码进行一些更改后,它可以正常显示 FsCalender 中的事件

  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 {
                                                self.EventsData.append(Event(
                                                    eventId: event["eventId"] as? String,
                                                    eventName:event["details"] as? String,
                                                    eventDate: event["date"] as? String
                                                    )
                                                )
                                            }

                                        }
                                        self.do_refresh()
                                    }


     func do_refresh()
        {
            DispatchQueue.main.async(execute: {

                self.calender.reloadData()

                return
            })
        }


    Fscalender Implementation 

    func calendar(_ calendar: FSCalendar, willDisplay cell: FSCalendarCell, for date: Date, at position: FSCalendarMonthPosition) {

            let dateFormatter3 = DateFormatter()

            dateFormatter3.dateFormat = "yyyy-MM-dd"
            let dateString3 = dateFormatter3.string(from: date)

            strcond  = dateString3 as NSString
        }


        func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
            let dateString = self.dateFormatter2.string(from: date)
            print("this count first ",self.EventsData.count)
            for d in EventsData{

                let date = d.eventDate
                let dateFormatter = DateFormatter()
                dateFormatter.dateFormat = "MM-dd-yyyy"
                let dateFromString : NSDate = dateFormatter.date(from: date!)! as NSDate
                dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
                let datenew = dateFormatter.string(from: dateFromString as Date)
                if datenew.contains(dateString) {
                    return 3
                }
            }
            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 = "MM-dd-yyyy"
                let dateFromString : NSDate = dateFormatter.date(from: date!)! as NSDate
                dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
                let datenew = dateFormatter.string(from: dateFromString as Date)


                if datenew.contains(dateString) {
                    return UIColor.init(red: 10, green: 200, blue: 399, alpha: 300)
                }
            }
            return nil
        }