尝试使用 JSon 数据重新加载 FSCalendar 但它崩溃了
Trying to Reload FSCalendar with JSon data but it crashes
此问题与我之前帐户的问题相关联。
代码
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: [=11=]["Date"] as! String) }.flatMap {
self.dateFormatter2.string(from:[=11=])
}
self.absentdays = Absentdetails.flatMap { dateFormatter.date(from: [=11=]["Date"] as! String) }.flatMap { self.dateFormatter2.string(from:[=11=]) }
DispatchQueue.main.async
{
self.calendar.reloadData()
}
}
}
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]: 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
将函数 getdateFromJSON()
替换为
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: [=10=]["Date"] as! String) }.flatMap {
self.dateFormatter1.string(from:[=10=])
}
self.absentdays = Absentdetails.flatMap { dateFormatter.date(from: [=10=]["Date"] as! String) }.flatMap { self.dateFormatter1.string(from:[=10=]) }
DispatchQueue.main.async
{
self.calendar.reloadData()
}
}
}
else
{
print("No data got from URL")
}
}
else{
print("error httpstatus code is :",httpstatus?.statusCode ?? "5")
}
}
task.resume()
}
请查看下面的附件图片是否是您想要的输出
此问题与我之前帐户的问题相关联。
代码
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: [=11=]["Date"] as! String) }.flatMap {
self.dateFormatter2.string(from:[=11=])
}
self.absentdays = Absentdetails.flatMap { dateFormatter.date(from: [=11=]["Date"] as! String) }.flatMap { self.dateFormatter2.string(from:[=11=]) }
DispatchQueue.main.async
{
self.calendar.reloadData()
}
}
}
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]: 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
将函数 getdateFromJSON()
替换为
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: [=10=]["Date"] as! String) }.flatMap {
self.dateFormatter1.string(from:[=10=])
}
self.absentdays = Absentdetails.flatMap { dateFormatter.date(from: [=10=]["Date"] as! String) }.flatMap { self.dateFormatter1.string(from:[=10=]) }
DispatchQueue.main.async
{
self.calendar.reloadData()
}
}
}
else
{
print("No data got from URL")
}
}
else{
print("error httpstatus code is :",httpstatus?.statusCode ?? "5")
}
}
task.resume()
}
请查看下面的附件图片是否是您想要的输出