Search code examples
iosswiftswift4alamofireimage

How to Solve This Error : Could not cast value of type '__NSCFString' (0x10354a248) to 'UIImage' (0x104c42b48)


When i run the code and click the segue to description view controller appear error in Debugging .Could not cast value of type '__NSCFString' (0x10354a248) to 'UIImage' (0x104c42b48). in main view controller error got this Thread 1: signal SIGABRT

MainViewController.swift

import UIKit import Alamofire import AlamofireImage import SDWebImage

class 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
    }
}

Solution

  • This line is the culprit:

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

    You are trying to cast the String as an UIImage.

    One solution to this is to add another key to foods[indexPath.row] called "photos" and make sure that it is a UIImage when you're assigning it.

    Or you could make a food struct like so:

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

    Replace your foods array's type to [Food]. This should provide more safety in the code and you won't have to force-cast everytime you want to get a photo.