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