From the Core Data Programming Guide (my emphasis):
从核心数据编程指南(我的重点):
By default, Core Data dynamically creates efficient public and primitive get and set accessor methods for modeled properties (attributes and relationships) of managed object classes.
默认情况下,Core Data会动态地为托管对象类的建模属性(属性和关系)创建有效的公共和原语获取和设置访问器方法。
Although I've been using Core Data and mogenerator happily since I started on Objective-C, I've never had a look at what this means, until an app submission got rejected due to alleged use of private API. Long story about a generated setPrimitiveTypeValue:
method, but not what my question is about.
虽然我从Objective-C开始就很高兴地使用Core Data和mogenerator,但我从来没有想过这意味着什么,直到一个应用提交被拒绝,因为我声称使用了私有API。关于生成的setPrimitiveTypeValue:方法的长话短说,但不是我要问的问题。
While reading the guide I stumbled on the idea of public and primitive accessors for entity attributes. Another quote, but further down:
在阅读指南时,我无意中发现了实体属性的公共和原始访问器的概念。这是另一句引文,但往下说:
For example, given an entity with an attribute
firstName
, Core Data automatically generatesfirstName
,setFirstName:
,primitiveFirstName
, andsetPrimitiveFirstName:
.例如,给定一个具有属性firstName的实体,Core Data将自动生成firstName、setFirstName:、primitiveFirstName和setPrimitiveFirstName:。
What are the primitive accessors for? Is it just so you can assign a BOOL value directly, without wrapping it in an NSNumber? If so, why would mogenerator have gone through the trouble of generating all kinds of <Attribute>Value
, set<Attribute>Value:
, primitive<Attribute>Value
, setPrimitive<Attribute>Value:
accessors?
原始访问器的作用是什么?是否只是为了让您可以直接分配BOOL值,而不将其包装成NSNumber?如果是这样,为什么mogenerator要麻烦地生成各种
I'm confused, who can help me out?
我很困惑,谁能帮我?
Related questions:
相关问题:
- Core Data Primitive Accessors (no answers)
- Core Data原语访问器(无答案)
- why would I need to use a primitive accessor methods in a core data project? (answered)
- 为什么我需要在核心数据项目中使用原始访问器方法?(回答)
2 个解决方案
#1
10
As far as I understand it, the primitive method does the actual getting and setting, but does not issue any KVC or KVO notifications (‘willAccessValueForKey‘ and so on). The public method calls the primitive method, but wraps the call in the KVO calls. So from outside the object, you'd typically call the public methods, but if you needed to use the properties of the object for internal reasons (to validate or derive some other property, for example) you would use the primitive methods to avoid firing all the notifications.
据我所知,原语方法执行实际的获取和设置,但不发出任何KVC或KVO通知(' willAccessValueForKey '等)。公共方法调用原语方法,但在KVO调用中包装调用。因此,通常从对象外部调用公共方法,但是如果出于内部原因需要使用对象的属性(例如,验证或派生其他属性),则可以使用基本方法来避免触发所有通知。
I'd welcome any corrections or clarifications on the answer as it is a subject I am interested in but not fully versed in.
我欢迎对答案的任何更正或澄清,因为这是我感兴趣但不完全精通的主题。
#2
0
@jrturton's answer covered issues regarding Custom Attribute and To-One Relationship Accessor Methods, where public-accessor is straightforward.
@jrturton的回答涵盖了关于自定义属性和To-One关系访问器方法的问题,其中公共访问器很简单。
In addition, to fully support To-Many Relationship Accessors, in custom implementation, public-accessors may have to call primitive-accessors in combination with NSMutableSet methods (unionSet: and minusSet:), which must be wrapped inside KVO method pairs ( will...did...).
此外,为了完全支持多对多的关系访问器,在自定义实现中,公共访问器可能必须结合使用nsmutableet方法(unionSet:和minusSet:)调用原始访问器,这些方法必须封装在KVO方法对中(will…did…)。
#1
10
As far as I understand it, the primitive method does the actual getting and setting, but does not issue any KVC or KVO notifications (‘willAccessValueForKey‘ and so on). The public method calls the primitive method, but wraps the call in the KVO calls. So from outside the object, you'd typically call the public methods, but if you needed to use the properties of the object for internal reasons (to validate or derive some other property, for example) you would use the primitive methods to avoid firing all the notifications.
据我所知,原语方法执行实际的获取和设置,但不发出任何KVC或KVO通知(' willAccessValueForKey '等)。公共方法调用原语方法,但在KVO调用中包装调用。因此,通常从对象外部调用公共方法,但是如果出于内部原因需要使用对象的属性(例如,验证或派生其他属性),则可以使用基本方法来避免触发所有通知。
I'd welcome any corrections or clarifications on the answer as it is a subject I am interested in but not fully versed in.
我欢迎对答案的任何更正或澄清,因为这是我感兴趣但不完全精通的主题。
#2
0
@jrturton's answer covered issues regarding Custom Attribute and To-One Relationship Accessor Methods, where public-accessor is straightforward.
@jrturton的回答涵盖了关于自定义属性和To-One关系访问器方法的问题,其中公共访问器很简单。
In addition, to fully support To-Many Relationship Accessors, in custom implementation, public-accessors may have to call primitive-accessors in combination with NSMutableSet methods (unionSet: and minusSet:), which must be wrapped inside KVO method pairs ( will...did...).
此外,为了完全支持多对多的关系访问器,在自定义实现中,公共访问器可能必须结合使用nsmutableet方法(unionSet:和minusSet:)调用原始访问器,这些方法必须封装在KVO方法对中(will…did…)。