0

我想知道如何更新从 tableView 中选择的 coredata 记录我的代码以保存 coredata 是:

- (IBAction)juardar:(id)sender {

NSUserDefaults *defs=[NSUserDefaults standardUserDefaults];

NSString *sedef_mat=[NSString stringWithFormat:@"%@",[defs objectForKey:@"Materia"]];
//NSLog(@"%@",sedef_mat);

NSManagedObjectContext *context = [self managedObjectContext];

// Create a new managed object
NSManagedObject *newtarea = [NSEntityDescription insertNewObjectForEntityForName:@"Tareas" inManagedObjectContext:context];
[newtarea setValue:self.Nombre.text forKey:@"nombre"];
[newtarea setValue:self.Descripcion.text forKey:@"descripcion"];
[newtarea setValue:sedef_mat forKey:@"materia"];

NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
    //NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}

}

谢谢

4

1 回答 1

1

只需设置新值,然后像上面一样保存上下文。但是您将使用获取的结果:

for (Tareas *object in fetchedResultsController.fetchedObjects)
{
    Tareas *t = (Tareas *)object;
    if ([t.nombre isEqualToString:@"NewValue1"])
    {
        Tareas.nombre = @"NewValue1";
        Tareas.description = @"NewValue2";
        Tareas.materia = @"NewValue3";
    }
}

然后像上面一样保存上下文:

if (![context save:&error])
    etc.
于 2013-04-03T23:29:27.000 回答