6
votes

I have set up a Core Data model where I have two objects, say Person and Address. A person has an address, and an address can belong to many people. I have modelled it in core data as such (so the double arrow points to Person, while the single arrow goes to Address)

I have then created two classes for those objects, and implemented some custom methods in those classes. In the Core Data model I have entered the names of the classes into them.

If I fetch an Address from Core Data directly, it gives me the actual concrete class and I can call my custom methods on it.

If on the other hand I fetch a Person and try to access the Address through Person (eg: person.address) I get back an NSManagedObject that is an address (eg: I can get to all the core data attributes I've set on it) but it doesn't respond to my custom methods, because it's of type NSManagedObject instead of Address. Is this a limitation of Core Data or am I doing something wrong? If it is a limitation are there any work arounds?

3
It's not a Core Data limitation; you are certainly doing something wrong. What exactly do you mean by "it doesn't respond to my custom methods, because it's of type NSManagedObject instead of Address"? Do you get a selector not recognized exception? Does the compiler complain? Show us some code and output or it will be hard to diagnose the problem.Barry Wark
So it's a runtime error (not a compiler one), about NSManagedObject not responding to the name of my custom method. The same error you get when you don't specify the name of your class in the core data modeller.rustyshelf

3 Answers

9
votes

Did you create those classes using the modeller (Select an Entity, File > new file.., Managed Object Class, then select the Model Entity)?

A while ago I had a similar problem because I didn't create my managed object models using the Modeller. What I did to make sure everything was up and running was to copy and save my custom methods (and everything else I'd implemented) and start from scratch using the modeller. Then I was able to customize my model classes again and everything worked just fine.

I know this is not a complete answer but perhaps it can help you until someone explains exactly what is going on.

Cheers!

6
votes

You probably just forgot to set the name of the class in the model when you created the entity - it defaults to NSManagedObject. Click on Person and Address in the modeller and check, on the far right side where the Entity properties are listed, that the Class field is filled in correctly with the name of the corresponding objective C class and isn't just the default NSManagedObject setting.

1
votes

Your implementation file for the class probably hasn't been added to the Target that you are running.

(Get Info on the .m file -> Check the targets tab)

If your xcdatamodel has the Class set, if it can't find it at run time it will still work, you will just get NSManagedObject instances back instead. Which will actually work just fine, until you try to add another method to the class, as you have found.