奇怪的 XCode 4.2 和核心数据行为

发布于 2024-12-10 17:39:01 字数 1201 浏览 0 评论 0原文

所以我最近升级到了 XCode 4.2,部分原因是我需要 IOS 5 SDK。

无论如何,当我编译并尝试运行我的应用程序时,我发现它不再运行。这是因为核心数据实体中的属性现在以某种方式标记为只读。

我没有这样做,除了错误消息之外我找不到任何证据。

在我的核心数据管理对象中,称为“注意:

@interface Note : NSManagedObject
@property (nonatomic, retain) NSString * title;
...


@implementation Note
@dynamic title;
...

稍后 - 以及其他地方 - 在代码中,我尝试执行以下操作:

self.note.title=self.noteTitle.text;

当执行到此行时,我收到这些错误:

Property 'title' is marked readonly on class 'Note'.  Cannot generate a setter method for it.
-[Note setTitle:]: unrecognized selector sent to instance 0x80ae3d0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Note setTitle:]: unrecognized selector sent to instance 0x80ae3d0'

什么????我可以手动编码设置器 - 错误表明它无法为我生成一个设置器 - 但我真的很想了解这里发生了什么。

我确实为该实体重新生成了托管对象文件,但这不起作用。我也创建了一个新的数据模型,并检查以确保没有设置任何不应该设置的内容。

编辑:我遗漏了一个重要的花絮:我在 Note(托管对象)上定义了一个类别。在类别中,我向对象添加了协议(MKAnnotation)。我的类别是这样定义的:

@interface Note (Extras) <MKAnnotation>

我已经删除了协议的“需要”,但没有删除引用。当我这样做时,一切都回到了应有的样子。

因此,虽然我已经解决了我的问题,但我还是想知道:向托管对象添加协议是不是不好的行为?

谢谢。

So I recently upgraded to XCode 4.2, in part because I needed the IOS 5 SDK.

Anyhow, when I compiled and attempted to run my app, I found that it would no longer run. This is due to the fact that an attribute in a Core Data entity is now somehow marked as read only.

I did not do this, and I can find no evidence of this other than the error message.

In my core data managed object, called Note:

@interface Note : NSManagedObject
@property (nonatomic, retain) NSString * title;
...


@implementation Note
@dynamic title;
...

Later - and elsewhere - in the code, I attempt to do this:

self.note.title=self.noteTitle.text;

When execution gets to this line, I receive these errors:

Property 'title' is marked readonly on class 'Note'.  Cannot generate a setter method for it.
-[Note setTitle:]: unrecognized selector sent to instance 0x80ae3d0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Note setTitle:]: unrecognized selector sent to instance 0x80ae3d0'

What the ???? I could just hand-code the setter - the error says it cannot generate one for me - but I'd really like to understand what happened here.

I did regenerate the managed object files for this entity, which did not work. I created a new data model, too, and checked to make sure nothing was set that shouldn't be.

Edit: I left out what turned out to be an important tidbit: I have a category defined on Note (the managed object). In the category, I added a protocol to the object (MKAnnotation). My category was defined thusly:

@interface Note (Extras) <MKAnnotation>

I had since removed the 'need' for the protocol, but did not remove the reference. When I did, everything went back to the way it should be.

So while I've solved my problem, I'm left wondering: Is it bad behavior to add a protocol to a managed object?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

软甜啾 2024-12-17 17:39:01

MKAnnotation 协议有一个只读 title 属性:

@property (nonatomic, readonly, copy) NSString *title

.. 它与同名的动态属性冲突。

(来源:Apple,MKAnnotation 协议参考)

The MKAnnotation protocol has a readonly title property:

@property (nonatomic, readonly, copy) NSString *title

.. which conflicts with your dynamic property by the same name.

(Source: Apple, MKAnnotation Protocol Reference)

微凉 2024-12-17 17:39:01

将协议添加到 NSManagedObject 中也不错;您想要这样做的原因有很多。

It is not bad to add protocols to a NSManagedObject; there are many reasons why you would want to.

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