我的UITableViewCells有问题
我创建了两个UITableView类。现在,我希望将其显示在TableView中。作为数据,我采用了一个天气API,该API应该显示两个不同的UITableViewCellen中的温度和最低温度]
但是,作为错误,我在第39行出现:
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
这是我的完整代码:
import UIKit
import Foundation
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
let minus: Double = 32.00
let session = URLSession.shared
let weatherURL = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=Ismaning&appid=2da51d7209b1151fc1bf22e761c88d4e")!
let dataTask = session.dataTask(with: weatherURL) {
(data: Data?, response: URLResponse?, error: Error?) in
if let error = error {
print("Error:\n\(error)")
} else {
if let data = data {
let dataString = String(data: data, encoding: String.Encoding.utf8)
print("All the weather data:\n\(dataString!)")
if let jsonObj = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary {
if let mainDictionary = jsonObj.value(forKey: "main") as? NSDictionary {
if var temperature = mainDictionary.value(forKey: "temp") {
temperature = (temperature as! Double - minus) / 1.8 / 10
DispatchQueue.main.async {
cell.aktuelleTemperatur.text = "\(temperature)°C"
}
}
} else {
print("Error: unable to find temperature in dictionary")
}
} else {
print("Error: unable to convert json data")
}
} else {
print("Error: did not receive data")
}
}
}
dataTask.resume()
return cell
}
else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellNr2") as! CustomCellNr2
let minus: Double = 32.00
let session = URLSession.shared
let weatherURL = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=Ismaning&appid=2da51d7209b1151fc1bf22e761c88d4e")!
let dataTask = session.dataTask(with: weatherURL) {
(data: Data?, response: URLResponse?, error: Error?) in
if let error = error {
print("Error:\n\(error)")
} else {
if let data = data {
let dataString = String(data: data, encoding: String.Encoding.utf8)
print("All the weather data:\n\(dataString!)")
if let jsonObj = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary {
if let mainDictionary = jsonObj.value(forKey: "main") as? NSDictionary {
if let temperature = mainDictionary.value(forKey: "temp") {
let temperature1: Double? = (temperature as! Double - minus) / 1.8 / 10
DispatchQueue.main.async {
cell.mindestTemperatur.text = String(format:"%.f", temperature1!) + "°C"
}
}
} else {
print("Error: unable to find temperature in dictionary")
}
} else {
print("Error: unable to convert json data")
}
} else {
print("Error: did not receive data")
}
}
}
dataTask.resume()
return cell
}
else {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "thirdCustomCell")
//set the data here
return cell
}
}
}
有人可以帮我解决这个问题吗?
先谢谢您
总是创建可选变量,以便应用程序在进行类型转换时永远不会崩溃
if var temperature = temperature1 as? Double{
temperature = (temperature - 10) / 1.8 / 10
}
显示此错误是针对可选值。要处理它们,我们使用
guard let temperature = (temperature as! Double - minus) / 1.8 / 10 else {return}
OR
if let temperature = (temperature as! Double - minus) / 1.8 / 10
另一种方法是检查并传递一个空值,
temperature = (temperature as! Double - minus) / 1.8 / 10 ?? 0.0