2011-08-22 87 views
2

我的数据模型核心数据。如何使用NSManagedObject子类中创建方法,我产生的子包和类

My data model

。在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]; 

} 

它不起作用,我在最后一行发生错误。如果我删除最后一行,它会起作用。 问题: 是正确的方法来使用这些生成的方法? 如何获取属于定义包的所有卡片?

+0

是的,建议使用xcode生成的方法。你遇到了什么错误 ? – Leonardo

+0

我收到终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因是:“ - [NSManagedObject addCardObject:]:无法识别的选择发送到实例0x4f42f60” – Michael

+0

问题是与模型。我之前删除了它,命名为ii,并且改名为后缀。现在我只是制造产品 - >清洁,它的工作原理!第二个问题仍然悬而未决。 – Michael

回答

1

你尝试实例化一个新的卡对象也是这样吗?

NSEntityDescription * description = [NSEntityDescription 
      entityForName:@"Card" inManagedObjectContext:self.context]; 

Card *card = [[Card alloc]initWithEntity:description 
      insertIntoManagedObjectContext:self.context]; 

然后,一旦你有一个临时卡对象,你可以像这样设置的值到它:

[card setValue: [[self.inputCardPack objectAtIndex:i] objectAtIndex:0] 
     forKey: "term"]; 

你可以去这个其他方式予以肯定,但因为讨厌的小@动态标识符,编译器会对你没有按照规定的方式设置“CoreData”中的值。基本上,你试图设置一个没有分配空间的属性的值,因为你没有@synthesize并分配它。

我不是哗(Objective-C的编译器)的内部运作100%,所以如果有人看到与我在说什么问题,请让我知道。这种方法似乎对我很有用。

希望这会有帮助

相关问题