0

我之前的问题是使用 2 个当前和缺席天数的数组在 FSCalendar 上简单地显示静态数据。

如何在 FSCalendar swift 3 中更改事件数组的背景颜色?

现在我想以 2array 的格式从以下 json 响应中获取当前和缺席日期:根据我之前的问题,现在和缺席想要在 FSCalendar 上显示。

如何解析相同?请指导我。提前致谢。

{
  "Present": [
  {
  "Student_ID": 2,
  "LeaveLetterApplied": null,
  "Message": null,
  "Date": "2017-06-04T00:00:00",
  "Notify": null,
  "Status": "Present"
 },
 {
  "Student_ID": 2,
  "LeaveLetterApplied": null,
  "Message": null,
  "Date": "2017-06-05T00:00:00",
  "Notify": null,
  "Status": "Present"
}],
  "Absent": [
   {
     "Student_ID": 2,
     "LeaveLetterApplied": "",
     "Message": "",
     "Date": "2017-06-01T00:00:00",
     "Notify": null,
     "Status": "Absent"
   },
   {
     "Student_ID": 2,
     "LeaveLetterApplied": "",
     "Message": "",
     "Date": "2017-06-02T00:00:00",
     "Notify": null,
     "Status": "Absent"
   },
  {
     "Student_ID": 2,
     "LeaveLetterApplied": "",
     "Message": "",
     "Date": "2017-06-03T00:00:00",
     "Notify": null,
     "Status": "Absent"
  },
  {
    "Student_ID": 2,
    "LeaveLetterApplied": "Applied",
    "Message": "Sick Leave",
    "Date": "2017-06-06T00:00:00",
    "Notify": null,
    "Status": "Absent"
 }
],
   "No_Of_Working_Days": 6,
   "No_Of_Present_Days": 2,
   "Percentage": 0
 }

下面是 m 用来解析的代码:

func getdateFromJSON()
{
    let url = NSURL(string: "http://ezschoolportalapi.azurewebsites.net/api/Student/AttendanceDetails?schoolid=1&studentid=1&month=1&year=2017")
    let request = NSMutableURLRequest(url: url! as URL)
    let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error)
        in
        guard error == nil && data != nil else
        {
            print("Error:",error)
            return
        }
        let httpstatus = response as? HTTPURLResponse
        if httpstatus?.statusCode == 200
        {
            if data?.count != 0
            {
                let responseString = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary
                let presentdetails = responseString["Present"] as? [[String:AnyObject]]
                let absentdetails = responseString["Absent"] as? [[String:AnyObject]]
                print(absentdetails)
                print(presentdetails)
               // dont know what to do next :(

                }

            else
            {
                print("No data got from URL")
            }
        }
        else{
            print("error httpstatus code is :",httpstatus?.statusCode)
        }

    }
    task.resume()

}

截屏外貌

错误日志:

2017-06-17 10:23:50.671 ezSchool[1848:24475]-[UICachedDeviceWhiteColor unsignedLongLongValue]:无法识别的选择器发送到实例 0x608000059ec0 2017-06-17 10:23:50.672 ezSchool[1848:24475] 无法设置( (FSCalendar)上的用户定义检查属性:-[UICachedDeviceWhiteColor unsignedLongLongValue]:无法识别的选择器发送到实例 0x608000059ec0 2017-06-17 10:23:50.672 ezSchool[1848:24475] -[UICachedDeviceWhiteColor unsignedLongLongValue]:无法识别的选择器 0 发送到实例 0x60170000201700 -06-17 10:23:50.672 ezSchool[1848:24475] 无法在 (FSCalendar) 上设置 (firstWeekday) 用户定义的检查属性:-[UICachedDeviceWhiteColor unsignedLongLongValue]: 无法识别的选择器发送到实例 0x608000059ec0 2017-06-17 10:23 :50.672 ezSchool[1848:24475] -[UICachedDeviceWhiteColor doubleValue]:无法识别的选择器发送到实例 0x608000059ec0 2017-06-17 10:23:50.673 ezSchool [1848:24475] 无法在(FSCalendar)上设置(headerHeight)用户定义的检查属性:-[UICachedDeviceWhiteColor doubleValue]:无法识别的选择器发送到实例 0x608000059ec0 2017 -06-17 10:23:50.673 ezSchool[1848:24475] -[UICachedDeviceWhiteColor doubleValue]:无法识别的选择器发送到实例 0x608000059ec0 2017-06-17 10:23:50.673 ezSchool[1848:24475] 无法设置(headerTitleTextSize)用户在(FSCalendar)上定义检查的属性:-[UICachedDeviceWhiteColor doubleValue]:无法识别的选择器发送到实例 0x608000059ec0 错误:可选(错误域 = NSURLErrorDomain 代码 = -1002“不支持的 URL”UserInfo = {NSUnderlyingError = 0x60000005cbc0 {错误域 = kCFErrorDomainCFNetwork 代码 = -第1002章NSErrorFailingURLStringKey=ezschoolportalapi.azurewebsites.net/api/Student/AttendanceDetails?schoolid=1&studentid=2&month=6&year=2017, NSErrorFailingURLKey=ezschoolportalapi.azurewebsites.net/api/Student/AttendanceDetails?schoolid=1&studentid=2&month=6&year=2017, NSLocalizedDescription=不支持的网址})

4

1 回答 1

0

不要NSDictionary在 Swift 中使用,您只需要使用DateFormatter来获取格式化日期。Swift 3 中的正确JSON字典也是[String:Any].

首先在你的类中声明两个实例属性,[String]名为presentDateArrayand absentDateArray

var presentDateArray = [String]()
var absentDateArray = [String]()

现在以这种方式在响应完成块中初始化这两个数组。

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

现在只需在您的方法委托中使用这两个数组

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
    }
}
于 2017-06-16T12:29:24.923 回答