Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode4: Different code generated for custom core data managed objects

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.

like image 256
pokstad Avatar asked Mar 09 '11 23:03

pokstad


2 Answers

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.

like image 170
Marcus S. Zarra Avatar answered Nov 15 '22 15:11

Marcus S. Zarra


It looks like all Xcode4 has done is make explicit what the @dynanmic directive makes implicit. The generated code looks exactly like that generated by Xcode3 when you asked it to generate accessors for a particular attribute (to the clipboard.)

I'm not sure why they decided to do it that way. Possibly they thought it would make it easier for people to create custom accessors if they just gave them the full basic accessor.

It shouldn't be that hard for someone to shoehorn MOGenerator into Xcode4. Hint, hint. Anyone? Come on guys! Anyone? Bueller?

Sigh, looks like it me.

like image 34
TechZen Avatar answered Nov 15 '22 14:11

TechZen