Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top