1

I have a Core Data model as follows, where children is a to-many relationship.

.h

@implementation MyEntity

@dynamic name;
@dynamic children;

@end

.m

@interface MyEntity : NSManagedObject

@property (nonatomic) NSString *name;
@property (nonatomic) NSOrderedSet *children;

@end

I then try to set it using:

        MYAppDelegate *delegate = (MYAppDelegate *)[UIApplication sharedApplication].delegate;
        NSManagedObjectContext *managedObjectContext = [delegate managedObjectContext];
        NSEntityDescription *categoryEntity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext];
        NSManagedObject *newCategory = [[NSManagedObject alloc] initWithEntity:categoryEntity insertIntoManagedObjectContext:managedObjectContext];
        [newCategory setValue:key forKey:@"name"];
        NSOrderedSet *testSet = [[NSOrderedSet alloc] initWithArray:@[@"This", @"is", @"a", @"test"]];
        [newCategory setValue:testSet forKey:@"children"];
    }
}

Yet on that last line, I get this error:

NSCFConstantString managedObjectContext]: unrecognized selector sent to instance 0xe8fa0'

If I change NSOrderedSet to NSSet the compiler complains that it expects an NSOrderedSet.

How can I assign the set to the NSManagedObject?

1 Answer 1

4

The problem isn't the NSOrderedSet, its the NSString instances that you put inside the set. These need to be replaces with instances of the entity which is configured in the data model at the destination of the relationship. You can't fill the relationship with the wrong kind of object.

2
  • The NSManagedObject represents a category. The category has a to-many relationship to itself, because I'm using a recursive hierarchy of categories. It seems then that elements of the NSOrderedSet should be instances of MyEntity. But how can I create those objects and children? Do I need to use an algorithm that populates the child entities first? Aug 6, 2013 at 7:25
  • As this is a test just alloc] initWithEntity:categoryEntity a new instance of your entity and put that in the set.
    – Wain
    Aug 6, 2013 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.