Property declaration for to-many relationships in Core Data

前端 未结 3 1560
我在风中等你
我在风中等你 2021-01-31 23:10

I have an application written using Core Data. I have 2 entities with a one-to-many relationship. I have subclassed NSManagedObject for both of them. The entity on the one-si

3条回答
  •  不知归路
    2021-01-31 23:47

    The simplest way to create .h and .m files for your CoreData entities is this:

    1. Select an entity in the data modeler.
    2. Press Command-N or select File->New File…
    3. Select 'Cocoa' from the source list.
    4. In the template chooser, you should now see an item called 'Managed Object Class'. If this isn't there, click Cancel and repeat steps 1-2.
    5. Press Next, choose the project/target, and press Next again.
    6. Now you see something like the following window: New Managed Object Class window http://blog.alanquatermain.net/images/ManagedObjectClass.png
    7. Select the options you need and click Finish.

    This will generate the following header and source files:

    Entity.h:

    #import 
    
    
    @interface Entity :  NSManagedObject  
    {
    }
    
    @property (retain) NSNumber * uniqueID;
    @property (retain) NSString * name;
    @property (retain) Entity * parent;
    @property (retain) NSSet* children;
    
    @end
    
    @interface Entity (CoreDataGeneratedAccessors)
    - (void)addChildrenObject:(Entity *)value;
    - (void)removeChildrenObject:(Entity *)value;
    - (void)addChildren:(NSSet *)value;
    - (void)removeChildren:(NSSet *)value;
    
    @end
    

    Entity.m:

    #import "Entity.h"
    
    
    @implementation Entity 
    
    @dynamic uniqueID;
    @dynamic name;
    @dynamic parent;
    @dynamic children;
    
    @end
    

    Note that the implementation doesn't contain anything except @dynamic markers to tell the compiler not to worry about missing methods/variables to match the properties.

提交回复
热议问题