Core Data 无法生成原始访问器

发布于 2024-10-08 11:21:35 字数 1257 浏览 0 评论 0原文

根据我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笨死的猪 2024-10-15 11:21:35

经过大量的绞尽脑汁、调试、摆弄和猜测和检查,我终于弄清楚了问题所在:如果将这些属性定义为实例变量,则核心数据基元访问器不会动态生成。我定义它们是为了调试目的(因为 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!

回忆追雨的时光 2024-10-15 11:21:35

我一直在研究这个问题,发现的一件事是,与文档相反,从数据模型生成的实现文件没有列出原始动态访问器。其他地方规定你必须自己添加它们。这可能是问题所在吗?

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?

醉态萌生 2024-10-15 11:21:35

您是否正在使用/修改 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.

Smile简单爱 2024-10-15 11:21:35

只是想说,我遇到了同样的问题,不得不根据您的评论切换到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文