0
votes

This is my code so far, with no errors, but it is not picking the dates from the 5 day forecast. What is wrong in this code?

//: to display the 5 day date array from the open weather API

enter code herevar temperatureArray: Array = Array() var dayNumber = 0 var readingNumber = 0 if let jsonObj = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary { if let mainArray = jsonObj!.value(forKey: "list") as? NSArray { for dict in mainArray { if let mainDictionary = (dict as! NSDictionary).value(forKey: "main") as? NSDictionary { if let temperature = mainDictionary.value(forKey: "temp_max") as? Double {

                                if readingNumber == 0 {
                          temperatureArray.append(temperature)
                      } else if temperature > temperatureArray[dayNumber] {
                            temperatureArray[dayNumber] = temperature



                                }

                            } else {
                   print("Error: unable to find temperature in dictionary")
                            }
                        } else {
                            print("Error: unable to find main dictionary")
                        }
                        readingNumber += 1
                        if readingNumber == 8 {
                            readingNumber = 0
                            dayNumber += 1


                        }
                        var dateArray: Array<String> = Array()
                        var dayNumber = 0
                        var readingNumber = 0

         if let weatherArray = jsonObj!.value(forKey: "list") as? NSArray {
                            for dict in weatherArray {

if let weatherDictionary = (dict as! NSDictionary).value(forKey: "list") as? NSDictionary { if let date = weatherDictionary.value(forKey: "dt_txt") as? String {

                                    if readingNumber == 0 {
                                  dateArray.append(date)
                                 } else if date > dateArray[dayNumber] {
                                    dateArray[dayNumber] = date
                                        }

                                    }

                                } else {
                  print("Error: unable to find date in dictionary")
                                }


                                readingNumber += 1
                                if readingNumber == 8 {
                                    readingNumber = 0
                                    dayNumber += 1


                                }
                            }
                        }
                    }
                }

            }





                    func fixTempForDisplay(temp: Double) -> String {
                        let temperature = round(temp)
               let temperatureString = String(format: "%.0f", temperature)
                        return temperatureString
                    }




                    DispatchQueue.main.async {

self.weatherLabel1.text = "Today: (fixTempForDisplay(temp: temperatureArray[0]))°C" self.weatherLabel2.text = "Tomorrow: (fixTempForDisplay(temp: temperatureArray[1]))°C" self.weatherLabel3.text = "Day 3: (fixTempForDisplay(temp: temperatureArray[2]))°C" self.weatherLabel4.text = "Day 4: (fixTempForDisplay(temp: temperatureArray[3]))°C" self.weatherLabel5.text = "Day 5: (fixTempForDisplay(temp: temperatureArray[4]))°C"

        func formatDate(date: NSDate) -> String {
            let dateFormatter = DateFormatter()
            dateFormatter.dateStyle = .medium
            return dateFormatter.string(from: date as Date)


    }



                self.dateLabel1.text = ": \(formatDate(date: dateArray[0]))"
                self.dateLabel2.text = ": \(formatDate(date: dateArray[1]))"
                self.dateLabel3.text = ": \(formatDate(date: dateArray[2]))"
                self.dateLabel4.text = ": \(formatDate(date: dateArray[3]))"
                self.dateLabel5.text = ": \(formatDate(date: dateArray[4]))"


            }




                }

                    }





    dataTask.resume()

}

}

1
It would help if we could see the json data for the icon part. The parameter you send to the UIImage constructor looks strange, are those really the names of your icons? - Joakim Danielson
What is this "iconArray, scale: [0]"? You know it's looking of an image named "iconArray, scale: [0]"? - bauerMusic
I pulled off the the "scale" that was a correct fix from Xcode, but still no success. For Joakim - "weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}] I have the 01d icon plus all the icons in my Xcode app. and I can pull the Icons on my View Controller for the daily weather using this self.iconImage.image = UIImage(named: weatherIcon) but it is the 5 day "Array" now this self.iconImage1.image = UIImage(named: "icon:[0]" ) self.iconImage2.image = UIImage(named: "icon:[1]") etc. that is still not working. - Rich J
also tried this: self.iconImage1.image = UIImage(named: ": (iconArray[0])") self.iconImage2.image = UIImage(named: ": (iconArray[1])") - Rich J

1 Answers

0
votes

It looks to me like you need to change your icon array to contain strings

var iconArray: Array<String> = Array()

and then when parsing the json text

if let icon = weatherDictionary.value(forKey: "icon") as? String {

and finally

self.iconImage1.image = UIImage(named: iconArray[0])
self.iconImage2.image = UIImage(named: iconArray[1])  

Of course the below comparison won't work anymore when icon is a string but I don't understand any of this if/else clasue so I don't know what to replace it with

if readingNumber == 0 {
  iconArray.append(icon)
} else if icon > iconArray[dayNumber] { //This won't work now. 
  iconArray[dayNumber] = icon
}