UITableViewCell从CoreData取数据,编辑页传值报错:NSManagedObject无法赋值给EIntentions?
Hey there, let's get this sorted out! The error you're seeing happens because when you fetch data from Core Data, it returns objects of type NSManagedObject by default—but you need to cast those to your specific entity subclass EIntentions to access its properties like eIntentionCell.
Here's how to fix this step by step:
1. First, confirm your Core Data entity is set up correctly
Make sure your EIntentions entity in the Core Data model has its class configured properly:
- Open your
.xcdatamodeldfile - Select the
EIntentionsentity - Go to the Data Model Inspector (right sidebar)
- Under Class, ensure the name is
EIntentionsand the Module is set toCurrent Product Module - For Codegen, choose either
Class DefinitionorCategory/Extension(this ensures Xcode generates the correct subclass for your entity)
2. Cast the selected object to EIntentions in didSelectRowAt
In your table view's selection method, you need to safely cast the selected NSManagedObject to EIntentions before passing it to your EditViewController.
If you're using a NSFetchedResultsController (common for Core Data table views):
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // Safely cast the fetched object to EIntentions guard let selectedIntention = fetchedResultsController.object(at: indexPath) as? EIntentions else { print("Error: Could not cast selected object to EIntentions") return } // Initialize your EditViewController let editVC = storyboard?.instantiateViewController(withIdentifier: "EditViewController") as! EditViewController // Pass the selected intention, and set the text field value directly editVC.selectedIntention = selectedIntention editVC.textField.text = selectedIntention.eIntentionCell // Present or push the view controller navigationController?.pushViewController(editVC, animated: true) }
If you're using a regular array as your data source:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { guard let selectedIntention = yourCoreDataArray[indexPath.row] as? EIntentions else { print("Error: Could not cast object to EIntentions") return } let editVC = EditViewController() editVC.textField.text = selectedIntention.eIntentionCell navigationController?.pushViewController(editVC, animated: true) }
3. Set up your EditViewController to receive the data
Make sure your EditViewController has the necessary properties to accept the data:
class EditViewController: UIViewController { @IBOutlet weak var textField: UITextField! var selectedIntention: EIntentions? // Optional: keep a reference if you need to update Core Data later override func viewDidLoad() { super.viewDidLoad() // Fallback in case you set the text here instead of before pushing if let intentionText = selectedIntention?.eIntentionCell { textField.text = intentionText } } }
Quick Troubleshooting Tip
If the cast still fails, double-check that:
- Your Core Data fetch request is set to return
EIntentionsobjects (you can set theentityproperty on the fetch request explicitly) - You haven't renamed the entity or its subclass without updating the model file
内容的提问来源于stack exchange,提问作者Anthony DeFallo

