pickwick
pickwick

Reputation: 3154

Obtaining entity name from NSMangedObject subclass class object

Is there a bulit-in way to obtain an entity name from the class object of an NSManagedObjectSubclass? I know that this can be readily determined from an instance of a subclass, but I want to ask the class itself. I can write a class function, but I would rather do this introspectively.

Upvotes: 18

Views: 5190

Answers (3)

Kaunteya
Kaunteya

Reputation: 3090

Add following code to your NSManagedObject subclass

class var entityName: String {
    NSStringFromClass(Self.self)
}

Eg:

@objc(Note)
public class Person: NSManagedObject {
    class var entityName: String {
        NSStringFromClass(Self.self)
    }
}

assert(Person.entityName == "Person")

Upvotes: 0

Vasily
Vasily

Reputation: 3790

You can do it now by executing NSManagedObject.entity().name where NSManagedObject() is your subclass.

Upvotes: 9

SpaceTrucker
SpaceTrucker

Reputation: 1168

Check out mogenerator if you haven't already. http://raptureinvenice.com/getting-started-with-mogenerator/

It adds a lot of missing features to core data. In particular it keeps you from having to regenerate your entity classes.

You could iterate thru the key values of the entities in the context:

[managedObjectContext registeredObjects];

Upvotes: 0

Related Questions