NSManageObject子类不能由alloc初始化?

时间:2016-01-22 09:09:51

标签: objective-c core-data

  

STEP:

首先,我在.xcdatamodelId中添加名为员工的实体,然后点击 - >产品 - >创建NSManageObject子类。

在第一步之后,我获得了Employee.h/.m,其中superClass为NSManageObjectEmployee+CoreDataProperties.h/.m

然后我想将员工的信息保存到核心数据,如Employee.m中所示:

+ (void)saveModelWithName:(NSString *)name startTime:(NSDate *)startTime birth:(NSDate *)birth {
Employee *employee = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:[[CoreDataManager sharedInstance] managedObjectContext]];
if (name) {
    employee.name = name;
    employee.startDate = startTime;
    employee.birth = birth;
}
NSError *error = nil;
if (![[[CoreDataManager sharedInstance] managedObjectContext] save:&error]) {
    NSAssert(NO, @"Error saving context:%@\n%@",[error localizedDescription],[error userInfo]);
}

}
数据已成功插入。我认为Employee与自定义类相同,所以 我想使用此类将数据保存在内存中并尝试初始化Employee

Employee *Sam = [[Employee alloc] init];

CoreData: error: Failed to call designated initializer on NSManagedObject class 'Employee'

我发现NSManageObject的文档说:

NSEntityDescription对象描述Core Data中的实体。实体是托管对象,类是id,或者 - 使用数据库类比 - 表是行。实例指定实体的名称,其属性(其属性和关系,由NSAttributeDescription和NSRelationshipDescription的实例表示)以及表示它的类。

似乎我无法使用NSManageObject类将模型保存在内存中 我是否必须自定义具有与Employee子类NSObject相同属性的类?
请给我一些建议。

1 个答案:

答案 0 :(得分:1)

您可以,但仅适用于插入上下文的实例。

所以:

  1. 您插入一个对象,然后可以保留对它的引用以便稍后使用(代码中为function getUtentes($nif, $nome, $idFrontOfficeSinalizador, $emailTecnico, $interesseProfissional, $Escolaridade, $situacaoEmprego,$estado){ $this->connect(); $idTecnico = -1; if ($emailTecnico != "") { $query = "select id from tecnicos where email = '$emailTecnico'"; $res = $this->execute($query); if (mysql_num_rows($res)>0){ $row = mysql_fetch_array($res); $idTecnico = $row[0]; } } $query = "select *, U.id as a from utentes U where estado=$estado "; if ($nome != "") $query .= " and nome like '%$nome%' "; if ($idFrontOfficeSinalizador != "" && $idFrontOfficeSinalizador != "-1") $query .= " and idFrontOfficeSinalizador = $idFrontOfficeSinalizador "; if ($nif != "") $query .= " and NIF = $nif "; if ($interesseProfissional != -1 && $interesseProfissional != "") $query .= " and (interesseProfissional1 = $interesseProfissional or interesseProfissional2 = $interesseProfissional or interesseProfissional3 = $interesseProfissional) "; if ($Escolaridade != -1 && $Escolaridade != "") $query .= " and idHabilitacoes = $Escolaridade "; switch ($situacaoEmprego) { case 1: $query .= " and empregado = 1"; break; case 2: $query .= " and empregado = 0"; break; case 3: $query .= " and Estudante = 1"; break; case 4: $query .= " and outraSituacao = 1"; break; default: break; } if ($idTecnico != -1) $query .= " and idTecnico = $idTecnico "; $query .= " order by U.nome asc "; //echo $query; $res = $this->execute($query); $this->disconnect(); return $res; } function getUtentesPorPagina($nif, $nome, $idFrontOfficeSinalizador, $emailTecnico, $interesseProfissional, $Escolaridade, $situacaoEmprego,$estado, $pagina){ $this->connect(); $idTecnico = -1; if ($emailTecnico != "") { $query = "select id from tecnicos where email = '$emailTecnico'"; $res = $this->execute($query); if (mysql_num_rows($res)>0){ $row = mysql_fetch_array($res); $idTecnico = $row[0]; } } $query = "select *, U.id as a from utentes U where estado=$estado "; if ($nome != "") $query .= " and nome like '%$nome%' "; if ($idFrontOfficeSinalizador != "" && $idFrontOfficeSinalizador != "-1") $query .= " and idFrontOfficeSinalizador = $idFrontOfficeSinalizador "; if ($nif != "") $query .= " and NIF = $nif "; if ($interesseProfissional != -1 && $interesseProfissional != "") $query .= " and (interesseProfissional1 = $interesseProfissional or interesseProfissional2 = $interesseProfissional or interesseProfissional3 = $interesseProfissional) "; if ($Escolaridade != -1 && $Escolaridade != "") $query .= " and idHabilitacoes = $Escolaridade "; switch ($situacaoEmprego) { case 1: $query .= " and empregado = 1"; break; case 2: $query .= " and empregado = 0"; break; case 3: $query .= " and Estudante = 1"; break; case 4: $query .= " and outraSituacao = 1"; break; default: break; } if ($idTecnico != -1) $query .= " and idTecnico = $idTecnico "; $query .= " order by U.nome asc "; $queryAux = $query; $pagina = ($pagina-1) * 15; $query .= " limit $pagina, 15 "; $res = $this->execute($query); /* if ( mysql_num_rows($res) == 0){ //se a página não devolver resultados, mostra a primeira página $pagina = 0; $queryAux .= " limit $pagina, 15 "; $res = $this->execute($queryAux); }*/ $this->disconnect(); return $res; } function getUtenteNIF($nif){ $query = "select *, U.id as a from utentes U where U.NIF = $nif"; $this->connect(); $res = $this->execute($query); $this->disconnect(); return $res; }
  2. 您使用employee从上下文中获取实例并保留以供日后使用
  3. 你'无法'创建一个未插入上下文的实例 - 这是你得到的错误的原因。您可以使用init方法,但它必须是NSFetchRequest(或其他在内部使用该方法的init方法)。

    以上是'不能',因为从技术上讲,你可以在调用init方法时为上下文传递initWithEntity:insertIntoManagedObjectContext:,但是你应该有很少的理由......