[英]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.