如何解决此错误:无法将类型“__NSCFString”(0x10354a248)的值转换为“UIImage”(0x104c42b48)
Posted
技术标签:
【中文标题】如何解决此错误:无法将类型“__NSCFString”(0x10354a248)的值转换为“UIImage”(0x104c42b48)【英文标题】:How to Solve This Error : Could not cast value of type '__NSCFString' (0x10354a248) to 'UIImage' (0x104c42b48) 【发布时间】:2019-02-19 21:35:45 【问题描述】:当我运行代码并单击 segue 以描述视图控制器时,调试中出现错误。无法将类型“__NSCFString”(0x10354a248)的值转换为“UIImage”(0x104c42b48)。 在主视图控制器错误得到这个线程1:信号SIGABRT
MainViewController.swift
导入 UIKit 进口阿拉莫火 导入 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
【问题讨论】:
【参考方案1】:这行是罪魁祸首:
vc.imagefoodsegue = foods[indexPath.row]["photoUrl"] as! UIImage
您正在尝试将String
转换为UIImage
。
对此的一种解决方案是向 foods[indexPath.row] 添加另一个名为“photos”的键,并在分配时确保它是 UIImage
。
或者你可以像这样制作一个食物结构:
struct Food
var name: String
var photoURL: URL
var photo: UIImage?
将您的 foods
数组类型替换为 [Food]
。这应该会在代码中提供更高的安全性,并且您不必每次想要获取照片时都强制投射。
【讨论】:
以上是关于如何解决此错误:无法将类型“__NSCFString”(0x10354a248)的值转换为“UIImage”(0x104c42b48)的主要内容,如果未能解决你的问题,请参考以下文章
求sql中解决连接错误 无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类 型