我正在开发 TODO 应用程序,它已全部完成并且运行良好,但突然开始出现错误“致命错误:在展开可选值时意外发现 nil”。需要一些指南!
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
@IBOutlet weak var tableView: UITableView!
var tasks : [Task] = [ ]
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(_ animated: Bool) {
getdata()
tableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let task = tasks[indexPath.row]
if task.isimportant{
cell.textLabel?.text = " ★ \(task.name!)"
}else{
cell.textLabel?.text = task.name!
}
return cell
}
func getdata() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do{
tasks = try context.fetch(Task.fetchRequest())
}
catch {
print ("Failed!")
}
}
}