我正在尝试与两个实体 - WardrobeList和Items实现Core Data to-many关系。 WardrobeList具有多对多关系" items",因此可以将多个项目添加到衣柜中。
我创建了NSManagedObject子类。这是他们的样子。
WardrobeList.h
@class Item;
@interface WardrobeList : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) BabyDetails *babydetails;
@property (nonatomic, retain) NSSet *items;
@end
@interface WardrobeList (CoreDataGeneratedAccessors)
- (void)addItemObject:(Item *)value;
- (void)removeItemObject:(Item *)value;
- (void)addItem:(NSSet *)values;
- (void)removeItem:(NSSet *)values;
@end
Item.h
@class WardrobeList;
@interface Item : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * brand;
@property (nonatomic, retain) NSString * color;
@property (nonatomic, retain) NSString * age;
@property (nonatomic, retain) NSDate * buydate;
@property (nonatomic, retain) NSNumber * quantity;
@property (nonatomic, retain) NSNumber * price;
@property (nonatomic, retain) NSData * image;
@property (nonatomic, retain) NSString * desc;
@property (nonatomic, retain) NSString * giftby;
@property (nonatomic, retain) NSString * category;
@property (nonatomic, retain) NSString * product;
@property (nonatomic, retain) WardrobeList *wardrobe;
@end
我有一个名为WardrobeList的表视图控制器,我在其中显示衣柜列表。我在这里有一个Add按钮,它将我带到Add Item视图控制器,用户可以在其中填充项目的详细信息。
我通过segue传递选定的衣柜对象。这是代码。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"AddItem"]) {
//Assign managed objects to dest view controller
AddItemViewController *destViewController = segue.destinationViewController;
destViewController.wardrobe = wardrobe;
} else if ([[segue identifier] isEqualToString:@"ShowItems"]) {
ItemsList *destViewController = segue.destinationViewController;
destViewController.wardrobe = wardrobe;
}
}
在AddItemVC.m中,我捕获用户输入的所有细节,并将它们添加到新的Item Managed Object中。然后我尝试将这个Item对象添加到我从segue收到的衣柜中,在我的保存按钮方法中。
- (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
// Create a new item object
Item *newItem = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:context];
if (!product) {
newItem.category = category;
product = [products objectAtIndex:[self.pickerView selectedRowInComponent:1]];
newItem.product = product;
}
//Saving the text fields
newItem.name = self.itemTextField.text;
newItem.brand = self.brandTextField.text;
newItem.age = self.ageTextField.text;
newItem.color = self.colorTextField.text;
newItem.quantity = @([self.quantityTextField.text intValue]);
newItem.price = @([self.priceTextField.text floatValue]);
//Save date
newItem.buydate = self.datePicker.date;
//Save image
newItem.image = imageData;
//Save description
newItem.desc = self.descTextField.text;
//Save the gift field values
if (self.giftTextField.text) {
newItem.giftby = self.giftTextField.text;
}
//Manage the relation between wardrobe and item
*[self.wardrobe addItemObject:newItem];*
newItem.wardrobe = self.wardrobe;
NSLog(@"Item saved");
}
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
执行save方法时收到此错误消息。
[WardrobeList addItemObject:]: unrecognized selector sent to instance 0x788d2880
有人可以指导我在这里做错了什么以及如何纠正?
一旦用户选择了WardrobeList中的特定衣柜,我最终会在新表视图中显示所选衣柜中的所有项目。
非常感谢您的帮助。感谢。
答案 0 :(得分:0)
您的CoreDataGeneratedAccessors
自动生成或输入了吗?如果我有关系,即
@property (nonatomic, retain) NSSet *rooms;
我的代码是使用项目名称生成的,而不是实体名称
- (void)addRoomsObject:(Room *)value;
(认识房间里的s)。如果不是这样,请查看您的模型(2个实体之间的正确连接,模型和类中的相同命名)或查看是否有更多错误信息