致命错误:解包可选值时意外发现 niloverride func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {if dialogs[indexPath.item].fromID == profileID {let cell = tableView.dequeueReusableCell(withIdentifier: "dialogMeCell", for: indexPath) as!对话单元格var 合作伙伴照片:UIImage?= 零如果让 imageData = partners[indexPath.item].userPhoto {partnerPhoto = UIImage(data: imageData as!Data)}cell.fillWithContent(partnerPhoto:partnerPhoto!,selfPhoto:profilePhoto!)返回单元格}别的 {let cell = tableView.dequeueReusableCell(withIdentifier: "dialogHimCell", for: indexPath) as!CellForDialogHimvar 合作伙伴照片:UIImage?= 零如果让 imageData = partners[indexPath.item].userPhoto {partnerPhoto = UIImage(data: imageData as!Data)}cell.fillWithContent(合作伙伴照片:合作伙伴照片!)返回单元格}解决方案:事实上,我还没有找到这个问题的具体解决方案,我只是将一些逻辑从模型移到了视图控制器.在模型中,我确实从 URL 加载到数据.现在我在 ViewController 中做对了,它工作得很好,但对我来说很奇怪,很奇怪,因为我刚刚做了 cmd-x cmd-v. 解决方案 将 imageData 投射到范围之前:if let imageData = partners[indexPath.row].userPhoto as?数据 {合作伙伴照片 = UIImage(数据:imageData)} 别的 {partnerPhoto = UIImage()//只是为了防止崩溃debugPrint("图像数据不正确")//您可以在这里设置某种占位符图像而不是像 UIImage(named: "placeholder.png") 这样的损坏的 imageData}cell.fillWithContent(partnerPhoto:partnerPhoto)返回单元格此外,如果您能提供更多详细信息 - profilePhoto 如何以及何时被初始化以及cell.fillWithContent(partnerPhoto: partnerPhoto!, selfPhoto: profilePhoto!) 代码.此外,您可以在 cell.fillWithContent 上设置断点,并在调用函数之前检查 partnerPhoto 和/或 profilePhoto 是否为零.Good day! It makes me mad, 'cos I do not understand what's going on. I've got 2 TableViewControllers with some similar logics. I work with 1 Data Model. This model contains user info and each entity always has a photo. To init it, I use the code below :var partnerPhoto: UIImage? = nilif let imageData = partners[indexPath.item].userPhoto { partnerPhoto = UIImage(data: imageData as! Data)}In debug partners[indexPath.item].userPhoto has real data and even imageData shows 4213 bytes.But, app crashes with typical error: fatal error: unexpectedly found nil while unwrapping an Optional valueEDIT:override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {if dialogs[indexPath.item].fromID == profileID { let cell = tableView.dequeueReusableCell(withIdentifier: "dialogMeCell", for: indexPath) as! CellForDialogMe var partnerPhoto: UIImage? = nil if let imageData = partners[indexPath.item].userPhoto { partnerPhoto = UIImage(data: imageData as! Data) } cell.fillWithContent(partnerPhoto: partnerPhoto!, selfPhoto: profilePhoto!) return cell}else { let cell = tableView.dequeueReusableCell(withIdentifier: "dialogHimCell", for: indexPath) as! CellForDialogHim var partnerPhoto: UIImage? = nil if let imageData = partners[indexPath.item].userPhoto { partnerPhoto = UIImage(data: imageData as! Data) } cell.fillWithContent(partnerPhoto: partnerPhoto!) return cell}SOLUTION:In fact I have not found concrete solution for this problem, I have just moved some logic from model to view controller. In model I did load from URL to data. Now I do it right in ViewController and it works perfect, but it is odd, very odd for me, 'cos I just did cmd-x cmd-v. 解决方案 Cast your imageData before the scope:if let imageData = partners[indexPath.row].userPhoto as? Data { partnerPhoto = UIImage(data: imageData)} else { partnerPhoto = UIImage() //just to prevent the crash debugPrint("imageData is incorrect") //You can set here some kind of placeholder image instead of broken imageData here like UIImage(named: "placeholder.png")}cell.fillWithContent(partnerPhoto: partnerPhoto)return cellAlso, it would be helpful if you'll provide more details - how and when profilePhoto is init-ed and cell.fillWithContent(partnerPhoto: partnerPhoto!, selfPhoto: profilePhoto!) code.Also, you can set breakpoint on your cell.fillWithContentand check if partnerPhoto and/or profilePhoto is nil before functions is called. 这篇关于带有真实数据的 UIImage init 返回 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-19 00:17