"The NSManagedObject of class" className "must have a valid NSEntityDescription value." error

I created a simple entity called 'CDWorkout' with one attribute 'name' inside CDModel.xcdatamodeld. The container name in AppDelegate is also "CDModel". The Codegen class for "CDWorkout" is a category / extension. Here is the code for the CDWorkout class:

class CDWorkout: NSManagedObject {

    class func createWorkout(workoutInfo : Workout, in context: NSManagedObjectContext) -> CDWorkout{
        let workout = CDWorkout(context: context)
        workout.name = "anyName"
        return workout
    }
}

The createWorkout function is called from another viewController with the context argument as container.viewContext, but is immediately reset with the message:

The application terminated due to the uncaught exception "NSInvalidArgumentException", reason: "NSManagedObject of class" Workout_Generator.CDWorkout "must have a valid NSEntityDescription value.

What did I forget?

+14
11

, Class Module: CDWorkout Entity.

+15

@objc .

@objc(CachedMovie)
public class CachedMovie: NSManagedObject {

@objc(CachedMovie)

+8

, . NSPersistentContainer .

, .xcdatamodeld. modelFileName.xcdatamodelId

let persistentContainer = NSPersistentContainer(name: "modelFileName")
+4

/ (SiriKit). , .xcdatamodeld, MyClass.entity().

, , :

  • @objc(MyClass) NSManagedObject
  • " ", " "
  • let entity = NSEntityDescription.entity(forEntityName: "MyClass", in: context)!
+3

enter image description here modeldataClass, , ,

+1

NSManagedObject,

let workout = NSEntityDescription.insertNewObjectForEntityForName("CDWorkout",
    inManagedObjectContext: context) as! CDWorkout

, : workout.name = "any_name"

CoreData CoreData​​p >

0

CoreData NSManagedObjectModel, Swift. NSEntityDescription.managedObjectClassName. Swift module .

:

let entity = NSEntityDescription()
entity.name = PostEntity.entityName // `PostEntity`
entity.managedObjectClassName = PostEntity.entityClassName // `MyFrameworkName.PostEntity`
entity.properties = [....]

: entityName entityClassName .

extension NSManagedObject {

    public static var entityName: String {
        let className = NSStringFromClass(self) // As alternative can be used `self.description()` or `String(describing: self)`
        let entityName = className.components(separatedBy: ".").last!
        return entityName
    }

    public static var entityClassName: String {
        let className = NSStringFromClass(self)
        return className
    }

}
0

CoreData , . .xcdatamodeld, . .xcdatamodeld, NSManagerObject .

0

.

@objc(Workout)

" ". , XCode, (EditorCreate NSManagedObject SubClass) , "MO", CoreData .

@objc(Workout) 

, :

class CDWorkoutMO: NSManagedObject {

 class func createWorkout(workoutInfo : Workout, in context: NSManagedObjectContext) -> CDWorkoutMO {
    let workout = CDWorkoutMO(context: context)
    workout.name = "anyName"
    return workout
}
}

XCode

0

, , , "NSManagedObject Subclass", . :

  • .xcdatamodeld
0

coreData , ,

enter image description here

MyStaticLibrary.MyManagedObject

, ,

0

Source: https://habr.com/ru/post/1682733/


All Articles