What happens when I don't write accessor methods for Core Data scalar property?

て烟熏妆下的殇ゞ 提交于 2019-12-13 19:02:59

问题


I am working with several Core Data entities that I used to generate NSManagedObject subclasses, and at the time I generated those classes I selected the "Use scalar properties for primitive data types" option. As expected, integer attributes were declared as int16_t properties and floating point attributes were declared as float properties in the generated interface:

@interface TeamStats:NSManagedObject
@property (nonatomic) int16_t pointsScored;
@property (nonatomic) float winPercentage;
@end

In the generated implementation for this interface, I see these properties declared with @dynamic:

@implementation TeamStats
@dynamic pointsScored;
@dynamic winPercentage;
@end

Now, according to the Core Data documentation, I should need to write accessor methods for these scalar properties because Core Data cannot generate them for me:

You can declare properties as scalar values, but for scalar values Core Data cannot dynamically generate accessor methods—you must provide your own implementations (see “Managed Object Accessor Methods” (page 44)). Core Data automatically synthesizes the primitive accessor methods (primitiveLength and setPrimitiveLength:), but you need to declare them to suppress compiler warnings.

This all makes sense, and I can see numerous SO posts asking about how these accessor methods should be written, with lots of good answers.

What I can't figure out is this: I wrote this code and started using it before I ever read this detail in the documentation, so I never wrote any accessor methods. I just have the @dynamic declarations in my class implementation. Despite not having written the accessor methods I should need, the code appears to work just fine. I have created instances of this NSManagedObject subclass. I have send instances of this class a setPointsScored: and a pointsScored message, and saved the resulting instance to its persistence store. Everything seems to work.

What am I missing here? Are there default implementations that are getting generated, despite what the Core Data documentation says? Are there going to be situations where my current code doesn't work that I need to be aware of? I would love to understand what is happening here before coding more NSManagedObject subclasses...


回答1:


Creating scalar accessors for primitive data types is a feature introduced with Xcode 4, but obviously not well-documented. It is mentioned in the WWDC 2011 Session videos

  • "303 What's New in Core Data on iOS" (slide 95)
  • "315 What's New in Core Data on Mac OS X" (slide 90)

According to the information from these sessions, the scalar accessors avoid the overhead of constructing NSNumber objects, so they are more efficient than the implementations shown in the "Core Data Programming Guide".

So to answer your question: Default accessors for the scalar properties are automatically generated. The "Core Data Programming Guide" is outdated with respect to this feature.



来源:https://stackoverflow.com/questions/14087218/what-happens-when-i-dont-write-accessor-methods-for-core-data-scalar-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!