Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSManagedObject subclass generated +CoreDataProperties.m files not actually required?

When generating an NSManagedObject subclass xcode creates two extra files MyObject+CoreDataProperties.h & MyObject+CoreDataProperties.m to keep the CoreData stuff away from our own code.

This is nice but I noticed in a WWDC clip that they didn't have the +CoreDataProperties.m file in their example. So I tried removing these in my code and everything compiles and runs fine without them, they don't appear to be necessary at all.

Are they required in some way that I've overlooked, or if not, then why does xcode generate them at all?

like image 339
trapper Avatar asked Oct 18 '22 08:10

trapper


1 Answers

The short answer:

No, it is not necessary.

The long answer:

Objective-C is a dynamic typing, late binding programming language. In a short form that means, that every type decision can be made at runtime instead of compile time and you can access properties of an and send messages to an object without knowing its type (class).

But it is not necessary for the Core Data framework and you as the user of Core Data and your model to know the type of an managed object for a entity type. It is even not necessary to have a specific type for an entity type. (I do not generate the classes very often and if I do, I do it manually.) Therefore in contrast to other programming languages these generated classes are not necessary to give the compiler a type.

However, the compiler wants to see at least every method at least one time to get the signature (parameter typing). Otherwise it would warn. Even it is possible to have working code like this …

NSManagedObject *person = …
NSString *firstName = [person firstName];

… for an entity type Person with a property firstName, the compiler will warn you, that he does not know anything about a method –firstName.

Instead you can type something like this:

NSManagedObject *person = …
NSString *firstName = [person valueForKey:@"firstName"];

(The compiler knows -valueForKey:, since this is a method declared in NSObject.)

Beside this you get benefits like code completion, check for typing errors and so on. But you do not need to use the code generation tool of Xcode. Simply declare such a class and at the properties at the interface. The accessors can be dynamically generated using @dynamic. (Personally I nearly never use the static code generation.)

Edit: Added the result of the discussion in the comments.

So having the interface (".h-file") of the category, the compiler knows enough to compile the whole code without warnings. This will work at runtime, if it is guaranteed – or checked at runtime – that a corresponding message can be dispatched. This makes sense in many situations from the very beginning of Objective-C, i. e. for forwarding and informal protocols. In the context of Core Data it is used to dynamically generate standard accessor methods. Everything works fine without any implementation.

However, one wants to have an implementation for some reasons, i. e. housekeeping on value changes. In such a case it is useful to have a stub implementation you can edit. But for standard behavior it is not necessary.

like image 61
Amin Negm-Awad Avatar answered Oct 21 '22 06:10

Amin Negm-Awad