NSManagedObject 无法识别的选择器发送到实例 [英] NSManagedObject unrecognized selector sent to instance

查看:54
本文介绍了NSManagedObject 无法识别的选择器发送到实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心数据模型如下,其中孩子是一对多的关系.

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

然后我尝试使用:

        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]:发送了无法识别的选择器到实例 0xe8fa0'

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

如果我将 NSOrderedSet 更改为 NSSet,编译器会抱怨它需要一个 NSOrderedSet.

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

如何将集合分配给 NSManagedObject?

推荐答案

问题不在于 NSOrderedSet,而是您放入集合中的 NSString 实例.这些需要替换为在关系目的地的数据模型中配置的实体的实例.你不能用错误的对象来填充关系.

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.

这篇关于NSManagedObject 无法识别的选择器发送到实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆