I am getting this error Thread 1: Exception: "-[Tasks initWithCoder:]: unrecognized selector sent to instance 0x60000034da40" whenever I try to load my core data object.
- I have two entities (Tasks, Goal) with inverse many to many relationship.
- The goal entity has an attribute of transformable with a custom class [NSManagedObject] of task. I think this creates an issue when loading. but surprisingly when I save my context it doesn't crash
Here us my subclass codegen of goal entity
extension Goal {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Goal> {
return NSFetchRequest<Goal>(entityName: "Goal")
}
@NSManaged public var date: String?
@NSManaged public var goalTasks: [NSManagedObject]?
@NSManaged public var isComplete: Bool
@NSManaged public var name: String?
@NSManaged public var nsdate: Date?
@NSManaged public var tasks: NSSet?
}
// MARK: Generated accessors for tasks
extension Goal {
@objc(addTasksObject:)
@NSManaged public func addToTasks(_ value: Tasks)
@objc(removeTasksObject:)
@NSManaged public func removeFromTasks(_ value: Tasks)
@objc(addTasks:)
@NSManaged public func addToTasks(_ values: NSSet)
@objc(removeTasks:)
@NSManaged public func removeFromTasks(_ values: NSSet)
}
Here us my subclass codegen of tasks entity
import Foundation
import CoreData
extension Tasks {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Tasks> {
return NSFetchRequest<Tasks>(entityName: "Tasks")
}
@NSManaged public var date: String?
@NSManaged public var importValue: Int16
@NSManaged public var isComplete: Bool
@NSManaged public var list: String?
@NSManaged public var name: String?
@NSManaged public var nsdate: Date?
@NSManaged public var goals: NSSet?
}
// MARK: Generated accessors for goals
extension Tasks {
@objc(addGoalsObject:)
@NSManaged public func addToGoals(_ value: Goal)
@objc(removeGoalsObject:)
@NSManaged public func removeFromGoals(_ value: Goal)
@objc(addGoals:)
@NSManaged public func addToGoals(_ values: NSSet)
@objc(removeGoals:)
@NSManaged public func removeFromGoals(_ values: NSSet)
}
here is how I am inserting a new goal object
@IBAction func setGoal(_ sender: Any) {
// Adding a task to the array
let df = DateFormatter()
df.dateFormat = "dd-MM-yyyy" // assigning the date format
let goalVC = GoalViewController()
let now = df.string(from: Date())
let newGoal = NSEntityDescription.insertNewObject(forEntityName: "Goal", into: context) as! Goal
newGoal.setValue(goalTitle.text!, forKey: "name")
newGoal.setValue(false, forKey: "isComplete")
newGoal.setValue(goalDate, forKey: "nsdate")
newGoal.setValue(now, forKey: "date")
newGoal.setValue(goalSubTasks, forKey: "goalTasks")
do {
try
context.save()
homeVC.loadGoals()
print(newGoal)
} catch {
print("Problem while saving")
}
self.dismiss(animated: true, completion: nil)
}
and here the app crashes when this function is called where I load my load my goals into allGoals (array of [NSManagedObject])
func loadGoals(){
allGoals.removeAll()
let requestGoal = NSFetchRequest<NSFetchRequestResult>(entityName: "Goal")
do {
allGoals = try context.fetch(requestGoal) as! [NSManagedObject]
print("loadTasks() fired!")
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
I think the issue lies in the Transformable goalTasks attribute in Goal Entity but I am not sure what to do with it. any help is appreciated!


NSManagedObjects(or subclasses) as transformable attributes - you should use relationships. But you have that as well (tasks). Why do you need thegoalTasksattribute? - pbasdftasksrelationship, which represents thetasksthat are related to thegoal? - pbasdf