首页 文章

我想在我的天气应用程序中显示一个图标数组,但似乎无法让UIImage显示它们

提问于
浏览
0

到目前为止,这是我的代码,没有错误,但它没有从5天预测中选择日期 . 这段代码有什么问题?

//:显示开放天气API的5天日期数组

enter code here var temperatureArray:Array = Array()var dayNumber = 0 var readingNumber = 0 if let jsonObj = try? JSONSerialization.jsonObject(带:data,options:.allowFragments)为? NSDictionary {如果让mainArray = jsonObj!.value(forKey:"list")为? NSArray {for mainArray中的dict {if let mainDictionary =(dict as!NSDictionary).value(forKey:"main")as? NSDictionary {if let temperature = mainDictionary.value(forKey:"temp_max")as?双{

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 {

如果让weatherDictionary =(dict as!NSDictionary).value(forKey:“list”)为? NSDictionary {if let date = weatherDictionary.value(forKey:“dt_txt”)as?字符串{

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 =“今天:(fixTempForDisplay(temp:temperatureArray [0]))°C”self.weatherLabel2.text =“明天:(fixTempForDisplay(temp:temperatureArray [1]))°C”self.weatherLabel3 . text =“第3天:(fixTempForDisplay(temp:temperatureArray [2]))°C”self.weatherLabel4.text =“第4天:(fixTempForDisplay(temp:temperatureArray [3]))°C”self.weatherLabel5.text = “第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 回答

  • 0

    在我看来,你需要更改你的图标数组以包含字符串

    var iconArray: Array<String> = Array()
    

    然后在解析json文本时

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

    最后

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

    当然,当icon是一个字符串时,下面的比较将不再起作用,但我不明白任何这个if / else clasue所以我不知道用什么替换它

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

相关问题