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

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

问题描述

现在Xcode4是公开的我把这个问题从苹果的秘密开发论坛:



有人可以解释为什么在以下过程中生成的代码是不同的比Xcode3?



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


  1. 转到模型编辑器

  2. 选择要为其生成源代码的实体

  3. 转到File-> New-> New Files

  4. 选择managedobject类(或其他类型,我无法再打开xcode3验证)

  5. 选择您要生成的实体(在步骤2中先前选择的实体已签出)

  6. 点击完成

现在,在Xcode4中,我想这是怎么做的,但我不确定,因为它生成不同的代码:


  1. 转到模型编辑器

  2. 选择实体

  3. 转到File-> New-> New File

  4. 选择NSManagedObject子类别

  5. 选择位置并创建。

它生成的是不同的原因有很多:


  1. 在实体中添加和删除集合的成员的生成代码不再在@interface中声明,而是@implementation。

  2. 现在,用于添加和删除对象的相同生成代码已完全定义,不再使用CoreDataGeneratedAccessors自动生成

  3. ol>

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

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



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

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

    有人可以权衡一下为什么不同? Xcode4代码感觉不喜欢这种新的生成NSManagedObject子类的方式。

    解决方案

    简短的答案:不要使用Xcode的代码生成。使用发电机,享受更轻松的生活。



    至于为什么,很难说。我从来不是Xcode生成Core Data子类的方式的粉丝,不会推荐它们。我们可以猜测为什么他们做了他们做的事情,但基于Xcode4和Core Data的其他问题,我会粉碎它未准备好或未完全测试。



    如果您想继续使用Xcode代码生成器,请写入雷达。


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

    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?

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

    1. Go to the model editor
    2. Select the entity you wish to generate source code for
    3. Go to File->New->New Files
    4. Choose managedobject class (or whatever it was, I can't open xcode3 anymore to verify)
    5. Select entities you wish to generate (the previously selected entity in step 2 is checked off)
    6. Click Finish

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

    1. Go to model editor
    2. Select entity
    3. Go to File->New->New File
    4. Choose "NSManagedObject subclass"
    5. Choose location and create.

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

    1. The generated code for adding and removing members of a set in the entity are no longer declared in the @interface, but instead @implementation. This causes code sense to fail detecting these methods.
    2. The same generated code for adding and removing objects is now fully defined, no longer autogenerated using CoreDataGeneratedAccessors

    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
    

    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];
    }
    

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

    解决方案

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

    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".

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

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

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