NSManagedObject自定义类--NSInvalidArgumentException无法识别的选择器 -

时间:2014-03-30 14:33:08

标签: core-data nsmanagedobject

我有一个看起来像这样的自定义NSManagedObject类。

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Ingredient, MenuCategory, Price, ItemSize;

@interface Item : NSManagedObject

@property (nonatomic, retain) NSNumber * collected;
@property (nonatomic, retain) NSString * desc;
@property (nonatomic, retain) NSString * instructions;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * quantity;
@property (nonatomic, retain) NSNumber * selected;
@property (nonatomic, retain) MenuCategory *menuCategory;
@property (nonatomic, retain) NSSet *prices;
@property (nonatomic, retain) NSSet *itemSizes;
@property (nonatomic, retain) NSSet *itemIngredients;


-(NSMutableSet *)mutablePrices;
-(NSMutableSet *)mutableItemIngredients;

@end

#import "Item.h"
#import "Ingredient.h"
#import "MenuCategory.h"
#import "Price.h"
#import "ItemSize.h"


@implementation Item

@dynamic collected;
@dynamic desc;
@dynamic instructions;
@dynamic name;
@dynamic quantity;
@dynamic selected;
@dynamic itemIngredients;
@dynamic menuCategory;
@dynamic prices;
@dynamic itemSizes;


-(NSMutableSet *)mutablePrices{

    return [self mutableSetValueForKey:@"prices"];
}

-(NSMutableSet *)mutableItemIngredients{

    return [self mutableSetValueForKey:@"itemIngredients"];
}

@end

没有什么特别的权利??? 以下应该正常工作????

[item.mutablePrices addObject:newPrice]

但它没有给我以下错误

2014-03-30 10:25:34.594 restos[1192:60b] -[NSManagedObject mutablePrices]: unrecognized selector sent to instance 0xe8d72c0
2014-03-30 10:25:34.597 restos[1192:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject mutablePrices]: unrecognized selector sent to instance 0xe8d72c0'

但我在做以下事情

[[item mutableSetValueForKey:@"prices"] addObject:newPrice];

工作非常精细---我知道有些原因很简单,我看不到它----

先谢谢你

1 个答案:

答案 0 :(得分:2)

错误消息

-[NSManagedObject mutablePrices]: unrecognized selector sent to instance 0xe8d72c0

表示您的item对象不是Item类的实例。可能的原因可能是您未在Core Data中将实体的设置为“Item” 模型检查员。

通常,如果让Xcode生成NSManagedObject子类文件(来自“编辑”菜单),并且在类别类别中添加自定义方法,则不易出错。或者,使用"mogenerator"