2
我的数据模型核心数据。如何使用NSManagedObject子类中创建方法,我产生的子包和类
。在Pack.m中,我可以看到以下内容
#import "Pack.h"
#import "Card.h"
@implementation Pack
@dynamic packTitle;
@dynamic card;
- (void)addCardObject:(Card *)value {
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"card" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"card"] addObject:value];
[self didChangeValueForKey:@"card" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[changedObjects release];
}
- (void)removeCardObject:(Card *)value {
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"card" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"card"] removeObject:value];
[self didChangeValueForKey:@"card" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
[changedObjects release];
}
- (void)addCard:(NSSet *)value {
[self willChangeValueForKey:@"card" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
[[self primitiveValueForKey:@"card"] unionSet:value];
[self didChangeValueForKey:@"card" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}
- (void)removeCard:(NSSet *)value {
[self willChangeValueForKey:@"card" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
[[self primitiveValueForKey:@"card"] minusSet:value];
[self didChangeValueForKey:@"card" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}
@end
所有这些方法都是由xcode生成的,并非公共的。我试图让第一个是公共方法 和写入数据库这样在我的viewController其中inputCardPack是数组的数组。
Pack* pack = [NSEntityDescription insertNewObjectForEntityForName:@"Pack" inManagedObjectContext:self.context];
pack.packTitle = self.inputCardPackTitle;
for (int i = 0; i< [self.inputCardPack count]; i++){
NSNumber *level = [NSNumber numberWithInt:0];
Card *card = [NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:self.context];
card.term = [[self.inputCardPack objectAtIndex:i] objectAtIndex:0];
card.def = [[self.inputCardPack objectAtIndex:i] objectAtIndex:1];
card.level = level;
//I'm trying to add a card to pack
[pack addCardObject:card];
}
它不起作用,我在最后一行发生错误。如果我删除最后一行,它会起作用。 问题: 是正确的方法来使用这些生成的方法? 如何获取属于定义包的所有卡片?
是的,建议使用xcode生成的方法。你遇到了什么错误 ? – Leonardo
我收到终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因是:“ - [NSManagedObject addCardObject:]:无法识别的选择发送到实例0x4f42f60” – Michael
问题是与模型。我之前删除了它,命名为ii,并且改名为后缀。现在我只是制造产品 - >清洁,它的工作原理!第二个问题仍然悬而未决。 – Michael