I am trying to pass data from the table view controller to a details table view controller with core data. I had a local data schema that was working but trying to switch it over and now I cannot compile the app. I get an error stating: Cannot assign value of type 'SetupEntity' to type '[String:Any Object]'
. The issue seems to be at the end on the seque and passing the data.
I am new to this so I hope I am explaining this right.
if I do print(toView.setup)
I get [:]
in the console.
If i do print(setups[indexPath.row])
I get this (which is the correct data):
<Setups.SetupEntity: 0x7fb4e1d608d0> (entity: SetupEntity; id: 0xd000000000040000 <x-coredata://435FF409-CFE7-4A1C-9312-504C98955F75/SetupEntity/p1> ; data: {
Title = "Title #7";
Name = "Your Name";
}
import UIKit
import CoreData
class SetupsTableTableViewController: UITableViewController {
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var setups = [SetupEntity]() //From CoreData
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
reloadData()
tableView.reloadData()
}
func reloadData() {
let fetchRequest = NSFetchRequest(entityName: "SetupEntity")
do {
if let results = try managedObjectContext.executeFetchRequest(fetchRequest) as? [SetupEntity] {
setups = results
}
} catch {
fatalError("There was an error fetching the list of setups!")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return setups.count //From CoreData
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("SetupsCell") as! SetupsTableViewCell
let setup = setups[indexPath.row]
cell.configureWithSetup(setup)
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("DetailsSegue", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
// MARK: - Misc
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "DetailsSegue" {
let toView = segue.destinationViewController as! DetailsTableViewController
let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
//This is where my problem is
toView.setup = setups[indexPath.row]
}
}
}