更新核心数据iOS

时间:2013-04-03 23:13:09

标签: ios xcode uitableview core-data nsmanagedobject

我想知道如何更新从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]);
}

}

由于

1 个答案:

答案 0 :(得分: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.