NSManagedObject unrecognized selector sent to instance

淺唱寂寞╮ 提交于 2019-12-11 07:36:10

问题


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:


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.



来源:https://stackoverflow.com/questions/18073716/nsmanagedobject-unrecognized-selector-sent-to-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!