Access NSManagedObject fields using KVC / valueForKey vs properties - which is better?

Writing my first application using CoreData. The book I use for guidance has this code:

// 'Person' is my managed object class Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext]; [newPerson setValue:nameField.text forKey:@"name"]; 

The book says that using a property style, for example

 newPerson.name = nameField.text; 

also works, but that "it very often happens that Core Data code uses the KVC approach"

For me, I see no reason to use the KVC approach; magic strings just cause runtime errors and it prints a lot more.

Speaking of which, I would like to know my β€œiPhone Way” habits in order to do something.

Is there a difference in these approaches and if most people use the first KVC approach ... why?

+4
source share
3 answers

Most people do not use the KVC approach that I saw; I am not for the reasons you describe.

To save your sanity, use the Mogenerator to create your accessories:

http://rentzsch.github.com/mogenerator/

This is a command line tool that generates proxy objects that can be used to extract CoreData objects, with some convenient methods - but even better, some category labels that you can add to your methods will not be destroyed, generate classes from your data model .

Xcode can also generate data objects from your model, but classes are simpler (just accessors), and mogenerator, I think, is easier to use repeatedly (which is important, since you will change the model a lot over time). Perhaps the next Xcode will be better in this regard.

I usually generate all classes of the data model in a subdirectory under the classes called "DataObjects" - then you can just re-add this entire directory every time you regenerate classes from the data model, which leads to the creation of new classes (when you have new entities ) An example command line command line looks like this:

  mogenerator -m ../MyProject.xcdatamodeld/MyProject-v1.xcdatamodel 

which will generate classes in the current directory from this data model (in this case I have a version with the very first version).

+3
source

Properties will lead to an increase in fractional performance compared to the direct use of KVC. However, KVC has its own characteristics, especially when working with keyPath , and not just with key .

KVC is also useful when you discover or dynamically access values ​​on an object.

For daily use, Kendall is definitely right, use movenerator and use properties. Easier to code, easier to maintain, etc. However, KVC definitely has its place and is extremely useful.

+1
source

name is an NSEntityDescription property, so newPerson.name great.

But when you add a custom property to your custom entity, it is known only at runtime - so newPerson.favouriteRestaurant will trigger a compile-time warning, even if it's normal.

This is annoying.

One way to get rid of it is to use

 [newPerson setValue:@"Crazy Maria's" forKey:@"favouriteRestaurant"] 

This can be a useful way to stop the compiler in several other scenarios when you use gong to use runtime magic.

0
source

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


All Articles