ios - 插入扩展抽象实体的实体时出错

标签 ios core-data

我有以下实体:

  • 用户。具有用户名、邮件等的普通用户实体。
  • ComputerUserWithAI。类似于用户,但它由应用程序控制。
  • 选择。包含与 Game 的关系,它应该包含一个选择器。

由于 Picker 可以是 UserComputerUserWithAI 我创建了一个名为 抽象实体 Picker 和我让 UserComputerWithAI 扩展了那个 Picker 类。 添加 Picker 实体后,我使 Pick 与名为 picker 的 Picker 实体建立了关系。

一切顺利,但我无法将 ComputerUserWithAI 插入到 Pick 中。我的代码是这样的:

ComputerUserWithAI *userWithAI = [NSEntityDescription insertNewObjectForEntityForName:@"ComputerUserWithAI" inManagedObjectContext:ctx];
userWithAI.name = @"DeepBlue";

Pick *pick = [NSEntityDescription insertNewObjectForEntityForName:@"Pick" inManagedObjectContext:ctx];
pick.game = game;
pick.picker = userWithAI;

我得到的错误如下:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for to-one relationship: property = "picker"; desired type = Picker; given type = NSManagedObject; value = (entity: ComputerUserWithAI...

但是当我这样做的时候:

pick.picker = [NSEntityDescription insertNewObjectForEntityForName:@"Picker" inManagedObjectContext:ctx];

它确实有效。

我检查了从核心数据生成的类,它们正确地扩展了抽象实体。可能出了什么问题?

最佳答案

发现错误。生成的 Pick.h 具有以下内容:

@class Picker
@interface Pick : NSManagedObject

@property (nonatomic, retain) NSManagedObject *game;
@property (nonatomic, retain) Picker *picker;

将其更改为:

@interface Pick : NSManagedObject

@property (nonatomic, retain) NSManagedObject *game;
@property (nonatomic, retain) NSManagedObject *picker;

使一切正常。

尝试验证 Xcode 生成的代码。这发生在我使用 Xcode 4.2 的时候。

关于ios - 插入扩展抽象实体的实体时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7462530/

相关文章:

ios/xcode/objective-c : Any way to link to other view controller in core-data driven page?

ios/xcode/coredata : How to mimic ajax call in objective c

iphone - 在应用程序委托(delegate)中添加持久存储(启用 iCloud)时崩溃

ios - 当在 View 中注入(inject)实体时,SwiftUI 预览版无法使用核心数据

ios - Cocos2d 调用父方法

ios - 无法识别的选择器发送到实例 0x7fb8bf50b3c0' (Swift/Xcode)

ios - 使用 QtCreator 构建应用程序 iOS 时出错

ios - 核心数据 :Fetch rlelationships or Fetch Main Entity

ios - 将 PFObject 设置为 UILabel

objective-c - 显示来自 NSDocumentDirectory 的照片