2011-08-22 85 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%,所以如果有人看到與我在說什麼問題,請讓我知道。這種方法似乎對我很有用。

希望這會有幫助

相關問題