Core Data 无法生成原始访问器
根据我对 Core Data 的理解,原始访问器工作所必需的只是属性名称的 @dynamic 指令(以及在实体实现中声明该属性的原始访问器)。
由于某种原因,当使用生成的基元访问器时,setState: 方法不会修改状态属性:
- (int)state
{
NSNumber * tmpValue;
[self willAccessValueForKey:@"state"];
tmpValue = [self primitiveState];
[self didAccessValueForKey:@"state"];
return [tmpValue intValue];
}
- (void)setState:(int)value
{
[self willChangeValueForKey:@"state"];
[self setPrimitiveState:[NSNumber numberWithInt:value]];
[self didChangeValueForKey:@"state"];
}
而使用键值编码版本确实修改状态
- (int)state
{
NSNumber * tmpValue;
[self willAccessValueForKey:@"state"];
tmpValue = [self primitiveValueForKey:@"state"];
[self didAccessValueForKey:@"state"];
return [tmpValue intValue];
}
- (void)setState:(int)value
{
[self willChangeValueForKey:@"state"];
[self setPrimitiveValue:[NSNumber numberWithInt:value] forKey:@"state"];
[self didChangeValueForKey:@"state"];
}
属性在这两种情况下,我的原始访问器声明如下(并且根据Apple的示例和代码生成):
@interface Post (CoreDataGeneratedPrimitiveAccessors)
- (NSNumber *)primitiveState;
- (void)setPrimitiveState:(NSNumber *)value;
@end
我有点不明白为什么会这样。任何帮助将不胜感激!
From my understanding of Core Data, all that is necessary for primitive accessors to work is the @dynamic directive for the property name (as well as declaring primitive accessors for that property within the entity implementation).
For some reason, when using the generated primitive accessor the setState: method is not modifying the state property:
- (int)state
{
NSNumber * tmpValue;
[self willAccessValueForKey:@"state"];
tmpValue = [self primitiveState];
[self didAccessValueForKey:@"state"];
return [tmpValue intValue];
}
- (void)setState:(int)value
{
[self willChangeValueForKey:@"state"];
[self setPrimitiveState:[NSNumber numberWithInt:value]];
[self didChangeValueForKey:@"state"];
}
while using the key-value-coding version does modify the state property
- (int)state
{
NSNumber * tmpValue;
[self willAccessValueForKey:@"state"];
tmpValue = [self primitiveValueForKey:@"state"];
[self didAccessValueForKey:@"state"];
return [tmpValue intValue];
}
- (void)setState:(int)value
{
[self willChangeValueForKey:@"state"];
[self setPrimitiveValue:[NSNumber numberWithInt:value] forKey:@"state"];
[self didChangeValueForKey:@"state"];
}
in both cases, I primitive accessors are declared as follows (and as per Apple's example and code generation):
@interface Post (CoreDataGeneratedPrimitiveAccessors)
- (NSNumber *)primitiveState;
- (void)setPrimitiveState:(NSNumber *)value;
@end
I'm a bit at a loss to why this would be. Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
经过大量的绞尽脑汁、调试、摆弄和猜测和检查,我终于弄清楚了问题所在:如果将这些属性定义为实例变量,则核心数据基元访问器不会动态生成。我定义它们是为了调试目的(因为 GBD 似乎无法在没有定义的 ivars 的情况下看到属性的值),这会阻止正确生成原始访问器。苹果确实应该以某种形式记录这一点。因为靠自己去发现是非常困难的。我希望这可以帮助其他遇到同样问题的人!
After tremendous amounts of head-scratching, debugging, fiddling and guess-and-check, I've finally figured out what the problem is: Core Data primitive accessors AREN'T dynamically generated if you define those attributes as instance variables. I had defined them for debugging purposes (as GBD cannot see the values of properties without defined ivars, it seems), and this prevented primitive accessors from being generated correctly. This is something that Apple should really document in some form. As it's very difficult to discover on one's own. I hope this helps others who've been having the same issue!
我一直在研究这个问题,发现的一件事是,与文档相反,从数据模型生成的实现文件没有列出原始动态访问器。其他地方规定你必须自己添加它们。这可能是问题所在吗?
I've been looking into this and one of the things discovered is that, contrary to docs, the implementation file generated from the data model does NOT list the primitive dynamic accessors. Other places state that you have to add them yourself. Could that be the issue?
您是否正在使用/修改 Xcode 生成的
NSManagedObject
代码?我相信默认情况下这些是由#if 0
指令“注释”生成的。Are you using/modifying the code of an
NSManagedObject
generated by Xcode? I believe that by default these are generated as "commented" out by an#if 0
directive.只是想说,我遇到了同样的问题,不得不根据您的评论切换到 setPrimitiveValue 和 PrimitiveValueForKey 。让我困扰的是默认实现不起作用。值得注意的是,我正在对另一个 NSManagedObject 进行子类化。不确定这是否也是你的情况。
Just wanted to say that I am having the same problem and had to switch to setPrimitiveValue and primitiveValueForKey based on your comment here. It bothers me that the default implementation does not work. Of note in my case is that I am subclassing another NSManagedObject. Not sure if that's your case as well.