在 swift 中使用 fscalendar 从事件的 API 响应中获取值

Fetch values from a API response for events using fscalendar in swift

我有 fscalendar,它显示了根据 API 响应预订的事件列表。 Json数据如下:

{
    "status": 200,
    "schedule_data": {
        "2020_08_16_1597485600": {
            "schedule_date": "2020-08-16",
            "nanny_id": 2,
            "shift_start": "06:00",
            "shift_end": "13:00",
            "schedule_id": 112
        },
        "2020_08_16": {
            "schedule_date": "2020-08-16",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_17_1597485600": {
            "schedule_date": "2020-08-17",
            "nanny_id": 2,
            "shift_start": "06:00",
            "shift_end": "22:00",
            "schedule_id": 113
        },
        "2020_08_17": {
            "schedule_date": "2020-08-17",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_18": {
            "schedule_date": "2020-08-18",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_19": {
            "schedule_date": "2020-08-19",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_20": {
            "schedule_date": "2020-08-20",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_21": {
            "schedule_date": "2020-08-21",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_22": {
            "schedule_date": "2020-08-22",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_23": {
            "schedule_date": "2020-08-23",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_24": {
            "schedule_date": "2020-08-24",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_25": {
            "schedule_date": "2020-08-25",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_26": {
            "schedule_date": "2020-08-26",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_27": {
            "schedule_date": "2020-08-27",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_28": {
            "schedule_date": "2020-08-28",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_29": {
            "schedule_date": "2020-08-29",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_30": {
            "schedule_date": "2020-08-30",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_31": {
            "schedule_date": "2020-08-31",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        }
    }
}

我已经根据 schedule_id 从这个 api 响应中获取事件,其中它不等于 zero.In 这样我有两个事件(16-8-2020, 17-8-2020)并且我已将它们存储在 array.The 事件中,这些事件使用 fscalendar 委托方法显示在 fscalendar 上,如下所示:

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {

    self.scheduleview.isHidden = false
    self.mainview.addSubview(self.scheduleview)
    self.todaydateLbl.text = "Availablity on " + self.formatter.string(from: date)
    self.selectdate = self.formatter.string(from: date)
    
      print("calendar did select date \(self.formatter.string(from: date))")
         if monthPosition == .previous || monthPosition == .next {
             calendar.setCurrentPage(date, animated: true)
         }

} 我有一个视图,当单击日期时会显示该视图,即 scheduleview。 如果事件已经存在,它会显示开始时间和结束时间值。 如何获取已经是 there.Like 的事件的开始和结束时间 例如,当用户选择 16-8-2020.He 时应该能够看到现有的事件详细信息,这将向他显示开始和结束时间从API response.How 做到了吗? json解析代码如下:

let decoder = JSONDecoder()
                      
                      do {
                          
                          let user = try decoder.decode(ScheduleListModel.self, from: response.data!)
                          self.listdata = user
                        
                        for schedule in self.listdata!.scheduleData {
                            if schedule.value.scheduleID != 0 {
                                self.eventsArray.append(schedule.value.scheduleDate!)
                            }
                        }
                        

                        print("eventsarray is",self.eventsArray)
                        
                        if self.eventsArray.count > 0
                        {
                            self.calendar.delegate = self
                            self.calendar.dataSource = self
                            self.calendar.reloadData()
                        }
                        

                         } catch {
                             debugPrint(error)
                      }

如何从可用的这些数据中获取详细信息?

您的 API 响应中有 "schedule_date": "2020-08-31",我假设您也将其保存在数据模型中。因此,当您从 calendar(_ calendar: FSCalendar, didSelect date: Date 方法获取所选日期时,将其转换为您保存在模型中的格式并过滤数据模型。

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let selectedDateString = formatter.string(from: date)

let selectedEvents = scheduleData.filter { ([=10=].value.scheduleId != 0) && ([=10=].value.scheduleDate == selectedDateString) }