injectionforxcode
injectionforxcode copied to clipboard
Could not cast in func prepare for segue
Steps to reproduce
- start project
- make injection with saving file
- make any push VC with implemented func prepare(for segue: , sender: ) and force cast in it. (see code and screenshots)
- error
Code:
import UIKit
class QController: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "QQQ" {
let vc = segue.destination as! ViewController
vc.view.backgroundColor = UIColor.black
}
}
}
class ViewController: UITableViewController {
@IBOutlet weak var someView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// someView.backgroundColor = UIColor.green
// someView.layer.cornerRadius = 30
// someView.layer.borderWidth = 10
// someView.layer.borderColor = UIColor.red.cgColor
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "Row \(indexPath.row + 1)"
// cell.textLabel?.font = UIFont.systemFont(ofSize: 30)
// cell.layer.borderWidth = 3
// cell.layer.borderColor = UIColor.magenta.cgColor
return cell
}
}
Error:
Could not cast value of type 'InjectionTest.ViewController' (0xb0de8) to 'InjectionTest.ViewController' (0xdb07314).
Screenshots:
This is a known problem I’m afraid with casting a class that has been injected.
Do you have any plans to fix it? I think it is very important.
Run into this situation too. Steps to reproduce:
- start xcode
- modify swift code of a view controller create from storyboard
- inject source file
- Recreate this controller from storyboard and force cast to its type.
You have to keep the definition of classes you are instantiating separate from classes you are injecting.