如何使此Web服务的API调用分别在swift中获取当前和不在日期的数组?

问题描述:

这是我以前的问题,简单地显示静态数据在FSCalendar上使用2天的现在和不存在的数组。如何使此Web服务的API调用分别在swift中获取当前和不在日期的数组?

How to change background color of events array in 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 
} 

下面是一个使用来解析代码米:

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

} 

Appearance截图

错误日志:

2017年6月17日10点23分: 50.671 ezSchool [1848:24475] - [UICachedDeviceWhiteColor unsignedLongLon无法识别的选择器发送到实例0x608000059ec0 2017-06-17 10:23:50.672 ezSchool [1848:24475]无法在(FSCalendar)上设置(placeholderType)用户定义的检查属性: - [UICachedDeviceWhiteColor unsignedLongLongValue]:无法识别的选择器已发送实例0x608000059ec0 2017-06-17 10:23:50.672 ezSchool [1848:24475] - [UICachedDeviceWhiteColor unsignedLongLongValue]:无法识别的选择器发送到实例0x608000059ec0 2017-06-17 10:23:50.672 ezSchool [1848:24475]失败(UICachedDeviceWhiteColor doubleValue):无法识别选择器发送到实例0x608000059ec0 2017-06-17 10:23:50.673 ezSc hool [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]未能在(FSCalendar)上设置(headerTitleTextSize)用户定义的检查属性: - [UICachedDeviceWhiteColor doubleValue]:无法识别的选择器发送到实例0x608000059ec0 错误:可选(错误域= NSURLErrorDomain代码= -1002“不支持的URL”UserInfo = {NSUnderlyingError = 0x60000005cbc0 {错误域= kCFErrorDomainCFNetwork代码= -1002“(null)”},NSErrorFailingURLStringKey = ezschoolportalapi.azurewebsites .net/api/Student/AttendanceDetails?schoolid = 1 & studentid = 2 &个月= 6 &年= 2017年,NSErrorFailingURLKey = ezschoolportalapi.azurewebsites.net/API /学生/ AttendanceDetails?schoolid = 1 & studentid = 2 &月= 6 &年= 2017年,NSLocalizedDescription =不支持URL})

+0

你想要什么方法的代表使用这个两阵,你在期待格式×2个阵列字符串与日期'2017-06-02'? –

+0

是的所有日期的存在和不存在于两个不同的阵列中,就像我在上一个问题中显示的那样。 – BugFinder

+0

嘿尼拉夫我应该怎么做在这个FSCalendar下面的方法。 filldefaultcolorsfor我在前面的问题中执行? – BugFinder

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

首先声明您的类[String]中的两个实例属性,名称为presentDateArrayabsentDateArray

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 
    } 
} 
+0

确定感谢好友将根据您的答案更新我的代码,并让您知道。:) – BugFinder

+0

@BugFinder好的会等待您的回复 –

+0

以及色彩效果如何?检查在我以前的问题的外观方法在这里https://*.com/questions/44581620/how-to-change-background-color-of-events-array-in-fscalendar-swift-3/44581711?noredirect=1 #comment76165119_44581711 – BugFinder