Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 核心数据。如何使用在NSManagedObject子类中创建的方法_Objective C_Core Data_Ios4 - Fatal编程技术网

Objective c 核心数据。如何使用在NSManagedObject子类中创建的方法

Objective c 核心数据。如何使用在NSManagedObject子类中创建的方法,objective-c,core-data,ios4,Objective C,Core Data,Ios4,我的数据模型 我生成了Pack和Class的子类。在Pack.m中,我可以看到以下内容 #import "Pack.h" #import "Card.h" @implementation Pack @dynamic packTitle; @dynamic card; - (void)addCardObject:(Card *)value { NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value c

我的数据模型

我生成了Pack和Class的子类。在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];

}
Pack*Pack=[NSEntityDescription insertNewObjectForEntityForName:@“Pack”inManagedObjectContext:self.context];
pack.packTitle=self.inputCardPackTitle;
对于(int i=0;i<[self.inputCardPack count];i++){
NSNumber*级别=[NSNumber NUMBER WITHINT:0];
Card*Card=[NSEntityDescription insertNewObjectForEntityForName:@“Card”在托管对象上下文中:self.context];
card.term=[[self.inputCardPack objectAtIndex:i]objectAtIndex:0];
card.def=[[self.inputCardPack objectAtIndex:i]objectAtIndex:1];
card.level=级别;
//我想在包装上加一张卡片
[打包添加卡片对象:卡片];
}
它不起作用,我在最后一行有个错误。如果我删除最后一行,它就会工作。 问题: 使用这些生成的方法是否正确?
如何获取属于已定义包的所有卡?

是否尝试像这样实例化一个新卡对象

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"];
您可以用其他方法来确定,但是由于这个讨厌的小@dynamic标识符,编译器会对您不按“CoreData”规定的方式设置值产生问题。基本上,您试图设置一个尚未分配空间的属性的值,因为您没有@synthesis并alloc它

我不是100%了解clang(objective-c编译器)的内部工作原理,所以如果有人看到我所说的有问题,请告诉我。这种方法似乎对我很有效


希望这有帮助

是的,建议使用xcode生成的方法。您遇到了什么错误?由于未捕获的异常“NSInvalidArgumentException”,我正在终止应用程序,原因:“-[NSManagedObject addCardObject:]:无法识别的选择器发送到实例0x4f42f60”模型有问题。我之前删除了它,命名为ii,之后改名为。现在我只做了产品->清洁,它的工作!第二个问题仍然悬而未决。