gpt4 book ai didi

ios - 更新 CoreData iOS

转载 作者:行者123 更新时间:2023-11-29 04:01:35 25 4
gpt4 key购买 nike

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

谢谢

最佳答案

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

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.

关于ios - 更新 CoreData iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15799865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com