加载 ViewController 时出现此错误。 我不知道我能做什么
loadDataFromDb函数是这样的
func loadDataFromDb() {
let fetchRequest = NSFetchRequest(entityName: "Chats")
daten = self.context!.executeFetchRequest(fetchRequest, error: nil) as! [Model]
tableView.reloadData()
}
我用谷歌搜索了这个错误,但没有任何帮助
最佳答案
dequeueReusableCellWithIdentifier返回一个可能为零的可选值。
通过使用 as!
强制转换为 UITableViewCell
,这样 nil 就会被转换为 UITableViewCell
并且最终调用nil 上的方法,这会导致异常。
尝试在使用之前使用 if let
来打开单元格,或者更好地使用 dequeueReusableCellWithIdentifier(_ identifier: String,
forIndexPath indexPath: NSIndexPath) -> AnyObject ,它不返回可选值。
总的来说,请尝试远离 !
并避免使用它,除非绝对必要。
编辑:
这是一种方法
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? UITableViewCell {
cell.textLabel?.text = "Text"
return cell
}
return UITableViewCell()
}
关于ios - xcode-线程 1 : EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31859561/