如何解决此错误:无法将类型'__NSCFString'(0x10354a248)的值强制转换为'UIImage'(0x104c42b48)

奥萨马(Amr Osama)

当我运行代码并单击segue to description视图控制器时,在Debugging中出现错误。无法将类型'__NSCFString'(0x10354a248)的值强制转换为'UIImage'(0x104c42b48)。在主视图控制器错误中得到此线程1:信号SIGABRT

MainViewController.swift

导入UIKit导入Alamofire导入AlamofireImage导入SDWebImage

类MainViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var tableView: UITableView?
var foods: [[String: Any]] = [[String: Any]]()


override func viewDidLoad() {
    super.viewDidLoad()
    Alamofire.request("https://api.myjson.com/bins/1bnsyj").responseJSON { (response) in
        if let responseValue = response.result.value as! [String: Any]? {
            print(responseValue)
            if let responseFoods = responseValue["items"] as! [[String: Any]]? {
                self.foods = responseFoods
                self.tableView?.reloadData()

            }
        }

        else {
            print("error : \(String(describing: response.result.error))")
        }
    }


    // Do any additional setup after loading the view.
} 
// MARK: - UITableViewDataSource & UITableViewDelegate

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return foods.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "FoodTableViewCell") as! FoodTableViewCell
    if foods.count > 0 {
        let eachFood = foods[indexPath.row]
        cell.lblFoodName?.text = (eachFood["name"] as? String) ?? ""
        cell.lblDescription?.text = (eachFood["description"] as? String) ?? ""

        let url = NSURL(string: self.foods[indexPath.row]["photoUrl"]! as! String)
        cell.imageViewFood?.af_setImage(withURL: url! as URL, placeholderImage: nil, filter: nil,runImageTransitionIfCached: true, completion: nil)

    }

    return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = storyboard?.instantiateViewController(withIdentifier: "DetailsViewController") as! DetailsViewController
    vc.foodnameseg = foods[indexPath.row]["name"] as! String
    vc.descriptionsegue = foods[indexPath.row]["description"] as! String
    vc.imagefoodsegue = foods[indexPath.row]["photoUrl"] as! UIImage
    self.navigationController?.pushViewController(vc, animated: true)
}

}

DetailsViewController.swift

import UIKit

class DetailsViewController: UIViewController {
    var foodnameseg : String = ""
    var descriptionsegue : String = ""
    var imagefoodsegue = UIImage()


    @IBOutlet weak var imagefood: UIImageView!
    @IBOutlet weak var lblNameSegue: UILabel!
    @IBOutlet weak var descriptionSegue: UITextView!
        override func viewDidLoad() {
        super.viewDidLoad()

         lblNameSegue.text = foodnameseg
         descriptionSegue.text = descriptionsegue
         imagefood.image = imagefoodsegue
    }
}
阿塔瓦·瓦迪亚

这是罪魁祸首:

vc.imagefoodsegue = foods[indexPath.row]["photoUrl"] as! UIImage

您正在尝试将转换StringUIImage

解决此问题的一种方法是在foods [indexPath.row]中添加另一个称为“照片”的密钥,并确保UIImage在分配该密钥时使用它。

或者,您可以像这样制作食物结构:

struct Food
{
  var name: String
  var photoURL: URL
  var photo: UIImage?
}

foods数组的类型替换[Food]这样可以在代码中提供更大的安全性,并且您不必每次想要获取照片时都强制播报。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章