多次解析Json时出错

时间:2018-07-18 17:41:25

标签: ios json swift parsing

多次解析JSON数据时遇到错误。下面的代码位在多次调用的函数中。如果该函数被调用一次,则该函数可以工作并加载数据,但是当我连续多次调用该函数时,它会随机失败。

我得到一个错误:

  

线程4:致命错误:展开一个可选值时意外发现nil

for(key, _) in jsonData!{行上。

有什么主意可以提高它的可靠性吗?

    struct financialData {
    var date: NSDate
    var open: String
    var high: String
    var low: String
    var close: String
    var volume: String

  }

func comparisonData(symbol:String){
    var comparisonDataArray = [financialData]()
    let apiKey: String = "SAMPLEAPIKEY"
    let timeLength: String = "TIME_SERIES_DAILY"
    let url = URL(string: "https://www.alphavantage.co/query?function=\ 
    (timeLength)&symbol=\(symbol)&outputsize=compact&apikey=\(apiKey)")!

    let task = URLSession.shared.dataTask(with: url) { (data, response,
        error) in
        if error != nil {
            print ("ERROR")
        } else {
            if let content = data {
                do {
                    let myJson = try JSONSerialization.jsonObject(with:
                        content, options: 
                    JSONSerialization.ReadingOptions.mutableContainers)
                        as AnyObject

                let jsonData = myJson["Time Series (Daily)"] as? [String:Any]

                    // loop that goes through days until todays date
                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "yyyy-MM-dd"

                    for(key, _) in jsonData!{
                       let date = key as String

                        if let cell = jsonData![date] as? NSDictionary {

                           let date = dateFormatter.date(from: date)! as NSDate
                           let open = cell["1. open"] as! String
                           let high = cell["2. high"] as! String
                           let low = cell["3. low"] as! String
                           let close = cell["4. close"] as! String
                           let volume = cell["5. volume"] as! String
                           let dataCell = financialData(date: date, open: open,
                                                         high: high, low: low, 
                        close: close, volume: volume)
                            comparisonDataArray.append(dataCell)
                        }
                    }
                    self.sortData()
                    self.arrayOfArrays.append(comparisonDataArray)
                }  catch  {
                    print(error.localizedDescription)
                }
            }
        }
    }
    task.resume()
}

这是我尝试使用结构解析并添加到数组的数据

    "Time Series (Daily)" =     {
    "2018-02-26" =         {
        "1. open" = "176.3500";
        "2. high" = "179.3900";
        "3. low" = "176.2100";
        "4. close" = "178.9700";
        "5. volume" = 36886432;
    };
    "2018-02-27" =         {
        "1. open" = "179.1000";
        "2. high" = "180.4800";
        "3. low" = "178.1600";
        "4. close" = "178.3900";
        "5. volume" = 38685165;
    };
    "2018-02-28" =         {
        "1. open" = "179.2600";
        "2. high" = "180.6150";
        "3. low" = "178.0500";
        "4. close" = "178.1200";
        "5. volume" = 33604574;
    };

0 个答案:

没有答案