Trying to Reload FSCalendar with JSon data but it crashes

一笑奈何 提交于 2019-12-30 13:32:11

问题


this question is linked with the my previous account's question.

How to make API call for this web service to fetch array of present and absent dates separately in swift?

code

import UIKit
import FSCalendar

class AttendenceViewController : UIViewController, FSCalendarDelegate, FSCalendarDataSource, FSCalendarDelegateAppearance
{

@IBOutlet weak var calendar: FSCalendar!

var presentdays : Array = [String]()
var absentdays : Array = [String]()
fileprivate let gregorian: Calendar = Calendar(identifier: .gregorian)
fileprivate lazy var dateFormatter1: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    return formatter
}()
fileprivate lazy var dateFormatter2: DateFormatter = {
    let formatter2 = DateFormatter()
    formatter2.dateFormat = "dd"
    return formatter2
}()

    override func viewDidLoad() {
    super.viewDidLoad()
        self.getdateFromJSON()


}



func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, fillDefaultColorFor date: Date) -> UIColor? {
    let datestring2 : String = dateFormatter1.string(from: date)
    if presentdays.contains(datestring2)
    {
        return UIColor.green
    }
    else if absentdays.contains(datestring2)
    {
        return UIColor.red
    }
    else
    {
        return nil
    }

}
func getdateFromJSON()
{
    let url = NSURL(string: "ezschoolportalapi.azurewebsites.net/api/Student/AttendanceDetails?schoolid=1&studentid=2&month=6&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
            {
                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.presentdays = presentdetails.flatMap { dateFormatter.date(from: $0["Date"] as! String) }.flatMap {
                        self.dateFormatter2.string(from:$0)
                    }
                    self.absentdays = Absentdetails.flatMap { dateFormatter.date(from: $0["Date"] as! String) }.flatMap { self.dateFormatter2.string(from:$0) }
                    DispatchQueue.main.async
                        {
                    self.calendar.reloadData()
                    }
                }
                }

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

    }
    task.resume()

}

 }

I'm attaching the error log here please help thanks in advance.

ERROR LOG

2017-06-17 10:23:50.671 ezSchool[1848:24475] -[UICachedDeviceWhiteColor unsignedLongLongValue]: unrecognized selector sent to instance 0x608000059ec0 2017-06-17 10:23:50.672 ezSchool[1848:24475] Failed to set (placeholderType) user defined inspected property on (FSCalendar): -[UICachedDeviceWhiteColor unsignedLongLongValue]: unrecognized selector sent to instance 0x608000059ec0 2017-06-17 10:23:50.672 ezSchool[1848:24475] -[UICachedDeviceWhiteColor unsignedLongLongValue]: unrecognized selector sent to instance 0x608000059ec0 2017-06-17 10:23:50.672 ezSchool[1848:24475] Failed to set (firstWeekday) user defined inspected property on (FSCalendar): -[UICachedDeviceWhiteColor unsignedLongLongValue]: unrecognized selector sent to instance 0x608000059ec0 2017-06-17 10:23:50.672 ezSchool[1848:24475] -[UICachedDeviceWhiteColor doubleValue]: unrecognized selector sent to instance 0x608000059ec0 2017-06-17 10:23:50.673 ezSchool[1848:24475] Failed to set (headerHeight) user defined inspected property on (FSCalendar): -[UICachedDeviceWhiteColor doubleValue]: unrecognized selector sent to instance 0x608000059ec0 2017-06-17 10:23:50.673 ezSchool[1848:24475] -[UICachedDeviceWhiteColor doubleValue]: unrecognized selector sent to instance 0x608000059ec0 2017-06-17 10:23:50.673 ezSchool[1848:24475] Failed to set (headerTitleTextSize) user defined inspected property on (FSCalendar): -[UICachedDeviceWhiteColor doubleValue]: unrecognized selector sent to instance 0x608000059ec0


回答1:


Replace the function getdateFromJSON() with

func getdateFromJSON()
        {
            let url = NSURL(string: "http://ezschoolportalapi.azurewebsites.net/api/Student/AttendanceDetails?schoolid=1&studentid=2&month=6&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 ?? "error")
                    return
                }
                let httpstatus = response as? HTTPURLResponse
                if httpstatus?.statusCode == 200
                {
                    if data?.count != 0
                    {
                        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.presentdays = presentdetails.flatMap { dateFormatter.date(from: $0["Date"] as! String) }.flatMap {
                                self.dateFormatter1.string(from:$0)
                            }
                            self.absentdays = Absentdetails.flatMap { dateFormatter.date(from: $0["Date"] as! String) }.flatMap { self.dateFormatter1.string(from:$0) }
                            DispatchQueue.main.async
                                {
                                    self.calendar.reloadData()
                            }
                        }
                    }
                    else
                    {
                        print("No data got from URL")
                    }
                }
                else{
                    print("error httpstatus code is :",httpstatus?.statusCode ?? "5")
                }

            }
            task.resume()
        }

Please see whether the below attached image is your desired output



来源:https://stackoverflow.com/questions/44601380/trying-to-reload-fscalendar-with-json-data-but-it-crashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!