Json在swift中解析后得到的空数据

时间:2017-08-24 19:10:39

标签: ios json swift swift3

我正在使用日历,它正常工作,所有来自JSON的事件我可以解析存储在数组中的JSON数据,如果我可以显示JSON数组显示为空滚动显示数据的日历JSON数组后, 这是代码

var EventsData = [Event]()

override func viewDidLoad() {
    super.viewDidLoad()
    getdairyDetails()
}
在getdairyDetails()

if errorCode == "0" {

    if let event_list = jsonData["events"] as? NSArray {

        self.EventDates.removeAll()

        for i in 0 ..< event_list.count {

            if let event = event_list[i] as? NSDictionary {

                self.compareDate(date: (event["date"] as? String)!)

                self.EventsData.append(
                    Event(
                        eventId: event["eventId"] as? String,
                        eventName:event["details"] as? String,
                        eventDate: event["date"] as? String
                    )
                )
            }
        }
        self.do_refresh()
    }

如果我可以调用此功能

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
    let dateString = self.dateFormatter2.string(from: date)
    print("this count first ",self.EventsData.count)
}


Result:
    this count first  0
    this count first  0
    this count first  0

After Scrolling the calender its showing 
    this count first  26
    this count first  26
    this count first  26

1 个答案:

答案 0 :(得分:1)

请尝试此代码

var  jsondarray = NSMutableArray()


override func viewDidLoad() {
    super.viewDidLoad()
    getdairyDetails()
}



func getdairyDetails()
{

    var userDic : [String : AnyObject] = [:]

    if errorCode == "0" {

        if let event_list = jsonData["events"] as? NSArray {


            for i in 0 ..< event_list.count {

                if let event = event_list[i] as? NSDictionary {

                    self.compareDate(date: (event["date"] as? String)!)

                    userDic = [
                        "eventId": event["eventId"] as? String,
                        "eventName":event["details"] as? String,
                        "eventDate": event["date"] as? String
                    ]

                    jsondarray.add(userDic)
               }
            }
        }

        self.do_refresh()
}

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
        let dateString = self.dateFormatter2.string(from: date)
        print("this count first ",self.jsondarray.count)
}