简体   繁体   English

我正在尝试让图标数组显示在我的天气应用中,但似乎无法获取UIImage来显示它们

[英]I am trying to get an icon array to display in my weather app, but can not seem to get UIImage to display them

This is my code so far, with no errors, but it is not picking the dates from the 5 day forecast. 到目前为止,这是我的代码,没有错误,但是没有从5天的预测中选择日期。 What is wrong in this code? 该代码有什么问题?

//: to display the 5 day date array from the open weather API //:显示来自开放天气API的5天日期数组

enter code here var temperatureArray: Array = Array() var dayNumber = 0 var readingNumber = 0 if let jsonObj = try? 如果让jsonObj =试试,请enter code here var temperatureArray:Array = Array()var dayNumber = 0 var readingNumber = 0 JSONSerialization.jsonObject(with: data, options: .allowFragments) as? JSONSerialization.jsonObject(with:data,options:.allowFragments)如? NSDictionary { if let mainArray = jsonObj!.value(forKey: "list") as? NSDictionary {如果让mainArray = jsonObj!.value(forKey:“ list”)设为? NSArray { for dict in mainArray { if let mainDictionary = (dict as! NSDictionary).value(forKey: "main") as? NSArray {用于mainArray中的字典{如果让mainDictionary =(字典为!NSDictionary).value(forKey:“ main”)为? NSDictionary { if let temperature = mainDictionary.value(forKey: "temp_max") as? NSDictionary {如果让温度= mainDictionary.value(forKey:“ temp_max”)为? 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? 如果让weatherDictionary =(字典为!NSDictionary).value(forKey:“ list”)为? NSDictionary { if let date = weatherDictionary.value(forKey: "dt_txt") as? NSDictionary {如果让日期= weatherDictionary.value(forKey:“ dt_txt”)为? 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" 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()

} }

} }

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 然后在解析json文本时

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 试图让我的CoreData显示在我的应用中 - Trying to get my CoreData to display in my app 我无法将球SKSpriteNode显示在辅助View Controller的UIImage(Xcode / Swift 2)上 - I can't get my ball SKSpriteNode to display on a secondary View Controller's UIImage(Xcode / Swift 2) 如何在单元格中显示UIImage数组? - How can I display UIImage array in cell? 如何通过“ created_time”在photosNSMutable数组中排序图像,并让我的收藏夹视图相应地显示它们? - How do I order my images in my photosNSMutable array by “created_time” and get my collection view to display them accordingly? 如何查询我的数据并将其按类别存储在数组中,以便在TableView中按类别显示它们? - How can I query my data & store them by category in an array so as to display them by category in a TableView? 如何在我的UITableView中使用我的UITextField显示键盘? - How can I get the keyboard to display with my UITextField in my UITableView? 如何在UIImageView中显示UIImage? - How can I display a UIImage in a UIImageView? 如何获取 UIDocumentPickerViewController 返回的 URL 的显示文件夹名称和应用程序图标? - How to get a display folder name and an App icon for an URL returned by UIDocumentPickerViewController? 我无法在第二个UIViewController中显示json数据 - I can not get the json data to display in my second UIViewController 从Web服务获取Unicode字符并在ios应用中显示它们 - Get unicode chars from webservice and display them in ios app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM