我对 Core Data 和 Objective-c 还是很陌生。我一直在上下核心数据文档,不知道我在这里做错了什么。
在运行时,将 NSManagedObject“ReportItem”添加到 NSManagedObject“RoomItem”中的 NSMutableSet“reports”时出现以下错误:NSUnknownKeyException', reason: '[<RoomItem 0x747c850> valueForUndefinedKey:]: the entity RoomItem is not key value coding-compliant for the key "reports"
错误发生在下面“creatReportForRoom”方法中的“RoomList”存储类内部,该方法在输入相关数据的 UIViewController 中的 viewWillDisapear 上调用:
- (ReportItem *)creatReportForRoom:(RoomItem *)currentRoom Report:(ReportItem *)report
{
NSDate *dateCreated = [NSDate date];
ReportItem *detailItem = [NSEntityDescription insertNewObjectForEntityForName:@"ReportItem" inManagedObjectContext:context];
[detailItem setDateReportCreated:dateCreated];
NSMutableSet *reports = [currentRoom mutableSetValueForKey:@"reports"];
[reports addObject:detailItem]; //error is occurring at this line
[allReports addObject:detailItem];
return detailItem;
}
“RoomItem”NSManagedObject 文件在这里:
房间物品.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface RoomItem : NSManagedObject
@property (nonatomic, strong) NSString *building;
@property (nonatomic, strong) NSString *room;
@property (nonatomic, retain) NSString *imageKey;
@property (nonatomic, retain) UIImage *buildingImage;
@property (nonatomic, strong) UIImage *buildingThumbnail;
@property (nonatomic, strong) NSData *buildingThumbnailData;
@property (nonatomic) double objectIndex;
@property (nonatomic, strong) NSDate *dateCreated;
@property (nonatomic, retain) NSMutableSet *reports;
@end
房间物品.m
#import "RoomItem.h"
@implementation RoomItem
@dynamic building, buildingThumbnail, buildingThumbnailData, objectIndex, room, dateCreated, imageKey, buildingImage, reports;
@end
我已经包括了“createItem”方法是创建和存储 RoomItem,以防我在那里做错了什么:
- (RoomItem *)createItem
{
double order;
//create new roomItem
//tracks what number item it's creating
if ([allItems count] == 0) {
order = 1.0;
}
else
{
order = [[allItems lastObject] objectIndex] + 1;
}
NSLog(@"Adding after %d items, order = %.2f", [allItems count], order);
RoomItem *detailItem = [NSEntityDescription insertNewObjectForEntityForName:@"RoomItem"
inManagedObjectContext:context];
[detailItem setObjectIndex:order];
[detailItem setDateCreated:[NSDate date]];
[allItems addObject:detailItem];
return detailItem;
}
具有关系的数据模型:
关系图:
任何有关解决此问题的建议将不胜感激。请让我知道是否有任何我未能包含的内容可能会有所帮助。
谢谢!