在 Xcode 中展开 UIImage(data: data) 时出现致命错误
Fatal Error while unwrapping UIImage(data: data) in Xcode
我有一个负责显示联系人列表的表视图,但是每当我尝试加载该表视图时,我的项目就会崩溃并出现错误|致命错误:在展开可选值时意外发现 nil|
这是我关于加载数据的代码现在的样子
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell",
for: indexPath) as!TableClass
let contacts = contact[indexPath.row]
cell.Name.text = contacts.name
cell.Picture.image = UIImage(data: contacts.image!, scale: 1) //this is the line of code that seems to make my project crash
return cell
}
我也尝试过使用这样的案例方法:
if case let cell.Picture.image = UIImage(data: contacts.image!, scale: 1) {
print("Hello")
}
然而,当我尝试此操作时,项目仍然崩溃并且 Xcode 指向 if case let
行并报告相同的错误
您应该使用可选绑定来获取可选图像。
if let image = contacts.image {
cell.Picture.image = UIImage(data: image, scale: 1)
} else {
//use default placeholder image
}
我有一个负责显示联系人列表的表视图,但是每当我尝试加载该表视图时,我的项目就会崩溃并出现错误|致命错误:在展开可选值时意外发现 nil| 这是我关于加载数据的代码现在的样子
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell",
for: indexPath) as!TableClass
let contacts = contact[indexPath.row]
cell.Name.text = contacts.name
cell.Picture.image = UIImage(data: contacts.image!, scale: 1) //this is the line of code that seems to make my project crash
return cell
}
我也尝试过使用这样的案例方法:
if case let cell.Picture.image = UIImage(data: contacts.image!, scale: 1) {
print("Hello")
}
然而,当我尝试此操作时,项目仍然崩溃并且 Xcode 指向 if case let
行并报告相同的错误
您应该使用可选绑定来获取可选图像。
if let image = contacts.image {
cell.Picture.image = UIImage(data: image, scale: 1)
} else {
//use default placeholder image
}