Prefer accessor property or KVC style to access Core Data properties

I read the chapters related to iOS, Marcus S. Zarra Basic data: data storage and management for iOS, OS X and iCloud (2nd edition) to great benefit. I am interested in one aspect, although the book suggests a style different from mine.

In the book examples, the attributes and relationships of NSManagedObject s' are available using KVC (for example, [recipe valueForKey: @"name"] ). There are (only) two brief explanations of how to find a definition of access to properties (for example, for use in recipe.name ). This seems to imply a clear recommendation by KVC in favor of accessory properties.

I usually like property accessors, for example. because they protect against errors caused by typos (for example, [recipe valueForKey: @"nam"] ), but because Xcode can generate the source code for me ( Editor | Create NSManagedObject Subclass ).

My question is this: are there technical reasons for why KVC should be used instead of the property access style?

+2
source share
1 answer

KVC is very useful, and the book emphasizes the use of KVC because, frankly, iOS development does not emphasize this enough , in my opinion. Understanding KVC is an extremely useful toolkit for iOS developers, and, unfortunately, its teaching has been left on the sidelines.

Having said that, for the reasons you have indicated, you should use property accessors. They catch errors during compilation, not runtime, and a little less.

KVC is useful when you are manipulating collections and very useful in dynamic situations or in situations where you do not necessarily know which object you are working with. These are areas in which point syntax fails pretty badly. The Dot syntax does not work with id , which, in my opinion, is in the cornerstone of Objective-C development.

In any case, there is no reason not to use property access devices, and I usually use them in my production code.

One final note: I do not use Xcode code generation. This has enough disadvantages that I recommend people use the mogenerator instead. You will have better results and a more convenient code base.

+3
source

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


All Articles