0

I have some doubts regarding storing plain "int" or "short" (int16_t) attributes inside the "Core Data" data management framework (on iOS 6 in my case). I tried using it various ways and found it impossible to do, failing with the above mentioned exception. Now, a few weeks later, I found this article inside StackOverflow and it looks like someone does exactly that:

See following example for storing an Enumeration in the lower answer from "Daniel Eggert": Best way to implement Enums with Core Data

Here it looks as if he uses a plain "int16_t" and maps it to a "Core Data" entry "Integer 16" inside the data model... does this really work? I tried it exactly as stated (even using the same naming :-)) and it fails with the well known "EXC_BAD_ACCESS" exception... as expected.

Any thoughts on this one? Did I misinterpret the linked answer?

---- Edit: --- Interesting how someone rates this question (that even properly links another question) down, but doesn't have an answer?! Anyway:

I now tried the very same with the "Event" sample application from Apple and it works. My own test doesn't work, still. Main difference between the apps:

  • My managed class is not auto-generated, I reused an existing class
  • I still have some properties with "@synthesize" in my existing class

My own entity (managed object): h:

@interface TestEntity : NSManagedObject
{
}
@property (nonatomic) int64_t testAttribute;
@end

m:

@dynamic testAttribute;

model data type: "Integer 64".

other.m:

TestEntity *testEntity = (TestEntity *)[NSEntityDescription insertNewObjectForEntityForName:@"Trip" inManagedObjectContext:[GenericDAO getManagedContext]];
[testEntity setTestAttribute : 4]; //this triggers the exception
4

2 回答 2

0

在上周详细讨论了整个问题后,我发现使用 iOS 6 和最新的 XCode(在我的情况下为 4.6.2)我可以完全按照我的问题中提到的链接中指定的方式执行所有操作。我看到的问题与一组根本没有记录的技术细节有关。我是怎么知道的?我只是执行了以下步骤来验证我的实体:

  • 使用支持 Core Data 的 Xcode 创建一个新的空项目
  • 在 Xcode 中使用“编辑器”菜单项“导入”,打开数据模型(选择要与 Core-Data 一起使用的“XY.xcdatamodeld”。
  • 您将看到在您的模型中导入了哪些项目,哪些是“错误”指定的(通过属性设置或其他)
  • 现在,只需创建您最初想要编码为原始数据类型的所有属性,然后在“编辑器”菜单中选择“导出”菜单项(选择数据模型时!)。小心:选中“创建原始数据类型”复选框以获取基本类型而不是 Cocoa/对象属性。

要检查您的原始项目,也在原始项目中执行此操作,在我的情况下,Core-Data 甚至菜单项看起来都不同(某些选项是灰色的):

  • 在旧项目/当前项目中执行相同的步骤(将实体导入数据模型) 。
  • 如果结果不同,这将告诉您您的项目设置在某种程度上不兼容/旧,您可以更改项目设置以找出哪些影响核心数据行为。
  • 还可以尝试使用导出功能来查看它是否正确生成了代码。

其他重要提示:您可以在 Core-Data 数据模型编辑器中选择属性并在项目之间复制/粘贴它们。如果您想迁移一些实体/属性以进行测试,这非常方便。不要只复制整个“cxdatamodeld”设置文件,它会保留旧项目的设置(我尝试了两次以验证)。

于 2013-05-25T16:15:00.493 回答
0

检查您为核心数据模型文件中的所有实体正确设置了名称、类和父实体。检查您的所有类名称是否与您的实体名称匹配。问题的原因是在编译模型时找不到带有setter的头文件。

于 2014-11-07T00:29:41.063 回答