Xcode4:为自定义核心数据托管对象生成的不同代码 [英] Xcode4: Different code generated for custom core data managed objects

查看:18
本文介绍了Xcode4:为自定义核心数据托管对象生成的不同代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在 Xcode4 是公开可用的,我将这个问题移出 Apple 的秘密开发论坛:

Now that Xcode4 is publicly available I'm moving this question out of Apple's secret dev forum:

有人能解释一下为什么下面程序中生成的代码与Xcode3中的不同吗?代码更好还是这可能是一个错误?

Can someone explain why the code generated in the following procedure is different than in Xcode3? Is the code better or might this be a bug?

我使用 Core Data 自定义托管类,这是我在 Xcode3 中遵循的过程:

I use Core Data custom managed classes and this was the procedure I followed in Xcode3:

  1. 转到模型编辑器
  2. 选择您要为其生成源代码的实体
  3. 转到文件->新建->新建文件
  4. 选择 managedobject 类(或者不管它是什么,我不能再打开 xcode3 来验证)
  5. 选择您要生成的实体(之前在第 2 步中选择的实体被选中)
  6. 点击完成

现在,在 Xcode4 中,我认为这是如何做到的,但我不确定,因为它会生成不同的代码:

Now, in Xcode4, I THINK this is how to do it, but I'm not sure because it generates different code:

  1. 转到模型编辑器
  2. 选择实体
  3. 转到文件->新建->新建文件
  4. 选择NSManagedObject 子类"
  5. 选择位置并创建.

由于多种原因,它生成的代码不同:

The code it is generating is different for a number of reasons:

  1. 用于在实体中添加和删除集合成员的生成代码不再在@interface 中声明,而是在@implementation 中声明.这会导致代码感知无法检测到这些方法.
  2. 现在完全定义了用于添加和删除对象的相同生成代码,不再使用 CoreDataGeneratedAccessors 自动生成

例如,Xcode3 会在 HEADER 文件中生成此代码:

For example, Xcode3 would have generated this code in the HEADER file:

@interface SampleEntity (CoreDataGeneratedAccessors)
- (void)addChildObject:(Child *)value;
- (void)removeChildObject:(Child *)value;
- (void)addChild:(NSSet *)value;
- (void)removeChild:(NSSet *)value;
@end

现在,Xcode4 在 IMPLEMENTATION 文件中生成此代码:

Now, Xcode4 generates this code in the IMPLEMENTATION file:

@implementation SampleEntity
@dynamic children;
- (void)addChildObject:(Child *)value {    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"children" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"children"] addObject:value];
    [self didChangeValueForKey:@"children" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

有人能谈谈为什么这不同吗?Xcode4 代码意识不喜欢这种生成 NSManagedObject 子类的新方式.

Can someone weigh in on why this is different? Xcode4 code sense does not like this new way of generating NSManagedObject subclasses.

推荐答案

简短回答:不要使用 Xcode 的代码生成.使用mogenerator,享受更轻松的生活.

Short answer: Don't use Xcode's code generation. Use mogenerator and enjoy an easier life.

至于为什么,很难说.我从来不喜欢 Xcode 生成 Core Data 子类的方式,也不会推荐它们.我们可以猜测他们为什么会做他们已经做过的事情,但基于 Xcode4 和 Core Data 的其他问题,我会将其归结为未准备好"或未完全测试".

As for the why, it is hard to say. I have never been a fan of the way that Xcode generates the Core Data subclasses and would not recommend them. We could guess as to why they did the things they have done but based on other issues with Xcode4 and Core Data I would chalk it up to "not ready" or "not fully tested".

如果您想继续使用 Xcode 代码生成器,请提交雷达.

File a radar if you would like to continue to use Xcode code generator.

这篇关于Xcode4:为自定义核心数据托管对象生成的不同代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆