【问题标题】:core data unrecognized selector sent to instance error: and Failed to call designated initializer on NSManagedObject class发送到实例错误的核心数据无法识别的选择器:并且无法在 NSManagedObject 类上调用指定的初始化程序
【发布时间】:2013-08-30 10:23:59
【问题描述】:

我已尝试使用以下代码更新核心数据中的对象,但出现错误。

    NSError *error;
    NSArray *objects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
    NSLog(@"objects %@",objects);


    // yourIdentifyingQualifier is unique. It just grabs the first object in the array.
    AllChallenge *tempChallenge = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] objectAtIndex:0];


    tempChallenge =[[AllChallenge alloc] init];
    NSLog(@"tempchallenge >>>>> %@",tempChallenge);

    // update the object

    tempChallenge.status = 1;



    [self.managedObjectContext save:&error];

编译后我得到CoreData:错误:无法调用NSManagedObject类'AllChallenge'上的指定初始化程序。谢谢你的帮助。

【问题讨论】:

  • objAllChallenge 来自哪里?你的意思是写tempChallenge
  • 是的,我的意思是 tempChallenge。
  • 问题是您是否在代码中使用 tempChallenge 或 objAllChallenge。不在问题中。

标签: iphone core-data


【解决方案1】:

如果你想更新一个 现有的 对象,那么你不应该用一个新的来替换它 分配一个:

AllChallenge *tempChallenge = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] objectAtIndex:0];
// tempChallenge =[[AllChallenge alloc] init]; // <-- REMOVE THIS LINE
tempChallenge.status = 1;
[self.managedObjectContext save:&error];

如果你想创建一个 new 对象那么你必须使用指定的初始化器, 或者这个方便的方法:

AllChallenge *tempChallenge = [NSEntityDescription insertNewObjectForEntityForName:@"AllChallenge" inManagedObjectContext: managedObjectContext];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 2013-01-18
    相关资源
    最近更新 更多