Reputation: 8149
I've got an instance variable array of full of Juice
objects that is subclassed from NSManagedObject
, retrieved from an NSFetch
.
I set the UITableViewCell
title like this, with no problem:
let currentJuice = juiceList[indexPath.row]
cell.text = currentJuice.name
However, I'm trying to pass an instance of Juice
between view controllers. I declare an instance variable var selectedJuice = Juice()
variable and then in the willSelectRowAtIndexPath
I put:
selectedJuice = juiceList[indexPath.row]
This line throws the error CoreData: error: Failed to call designated initializer on NSManagedObject class
and I can't figure out why! I also don't know how to implement the designated init method in the Juice
class, if anyone can help with that one
Upvotes: 2
Views: 405
Reputation: 539805
The problem is that Juice()
creates a Juice object without using the designated
initialiser of NSManagedObject
. And actually you don't need to assign a default
value to the property at all.
You should declare the property as an optional
var selectedJuice : Juice? // (1)
or implicitly unwrapped optional
var selectedJuice : Juice! // (2)
Then assigning
selectedJuice = juiceList[indexPath.row]
should work without problem.
In the first case (1), you would access the property as
if let theJuice = selectedJuice {
// ...
} else {
// no Juice selected
}
In the second case (2), you would access the property simple as selectedJuice
,
and you get a runtime exception if it is not set (nil
).
Upvotes: 5