【发布时间】:2014-05-04 17:13:53
【问题描述】:
我正在尝试将一些数据保存到 CoreData,但出现错误:
Restaurant Manager[5971:60b] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[ProdusComandat setIdProdus:]:无法识别的选择器已发送到实例 0x90b8270”
谁能告诉我为什么?
SessionController* sessionController = [[SessionController alloc]init];
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"ComenziActive"];// baza de date
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"numeClient == %@",sessionController.getSessionUsername]];
NSError *err;
NSUInteger count = [self.managedObjectContext countForFetchRequest:fetchRequest error:&err];
NSArray *listaElementeBD = [[NSArray alloc]init];
if(count <= 0)
{
NSEntityDescription *entitydesc;
entitydesc = [NSEntityDescription entityForName:@"ComenziActive" inManagedObjectContext:self.managedObjectContext];
ComenziActive *comanda = [NSEntityDescription insertNewObjectForEntityForName:@"ComenziActive" inManagedObjectContext:self.managedObjectContext];
[comanda setValue:sessionController.getSessionUsername forKey:@"numeClient"];
ProdusComandat * produsComandat = [[ProdusComandat alloc]init];
produsComandat.idProdus = [[[(Produs*)produsdb objectID] URIRepresentation] absoluteString];
produsComandat.cantitateComandata = [NSNumber numberWithInteger:[self.cantitateTextField.text integerValue]];
[comanda addProduseComandateObject:produsComandat];
}
else
{
NSError *error;
listaElementeBD = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
ComenziActive *comanda = [listaElementeBD objectAtIndex: 0];
NSArray *produse = comanda.produseComandate.allObjects;
BOOL found =NO;
for(int i=0;i<[produse count];i++)
{
ProdusComandat *p =[[ProdusComandat alloc]init];
p= produse[i];
if ([p.idProdus isEqualToString:[[[(Produs*)produsdb objectID] URIRepresentation] absoluteString]]) {
p.cantitateComandata = [NSNumber numberWithInteger:[p.cantitateComandata integerValue] + [self.cantitateTextField.text integerValue]];
found = YES;
break;
}
}
if (!found) {
ProdusComandat * produsComandat = [[ProdusComandat alloc]init];
produsComandat.idProdus = [[[(Produs*)produsdb objectID] URIRepresentation] absoluteString];
produsComandat.cantitateComandata = [NSNumber numberWithInteger:[self.cantitateTextField.text integerValue]];
[comanda addProduseComandateObject:produsComandat];
}
NSLog(@"============================");
NSLog(@"%@", comanda.produseComandate );
}
NSError *error;
[self.managedObjectContext save:&error];
我要做的是从 CoreData 实体 Produs 获取数据并将其 objectID 复制到实体 ProduseComandate 具有这种关系 ComenziActive>ProduseComandate(关系一对多)如果 objectID 不存在于表中,但如果它确实存在,我想将我从 TextField 中获取的一些值添加到值 cantiate。
【问题讨论】:
-
显示实体和类定义。
idProdus是如何定义的? -
同时显示 ProductComandat 类
标签: ios core-data runtime-error