objective-c 以编程方式更新 Core Data 中的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/431797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically Update an attribute in Core Data
提问by macinjosh
I've looked through all the class documentation for Core Data and I can't find away to programmatically update values in a core data entity. For example, I have a structure similar to this:
我已经浏览了 Core Data 的所有类文档,但找不到以编程方式更新核心数据实体中的值。例如,我有一个类似于这样的结构:
id | title
============
1 | Foo
2 | Bar
3 | FooFoo
Say that I want to update Bar to BarBar, I can't find any way to do this in any of the documentation.
假设我想将 Bar 更新为 BarBar,我在任何文档中都找不到任何方法来执行此操作。
回答by giff
In Core Data, an object is an object is an object - the database isn't a thing you throw commands at.
在 Core Data 中,一个对象就是一个对象就是一个对象——数据库不是你抛出命令的东西。
To update something that is persisted, you recreate it as an object, update it, and save it.
要更新持久化的内容,您可以将其重新创建为对象、更新并保存它。
NSError *error = nil;
//This is your NSManagedObject subclass
Books * aBook = nil;
//Set up to get the thing you want to update
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"MyLibrary" inManagedObjectContext:context]];
[request setPredicate:[NSPredicate predicateWithFormat:@"Title=%@",@"Bar"]];
//Ask for it
aBook = [[context executeFetchRequest:request error:&error] lastObject];
[request release];
if (error) {
//Handle any errors
}
if (!aBook) {
//Nothing there to update
}
//Update the object
aBook.Title = @"BarBar";
//Save it
error = nil;
if (![context save:&error]) {
//Handle any error with the saving of the context
}
回答by Evan DiBiase
The Apple documentation on using managed objects in Core Datalikely has your answer. In short, though, you should be able to do something like this:
关于在 Core Data 中使用托管对象的Apple 文档可能有你的答案。简而言之,您应该能够执行以下操作:
NSError *saveError;
[bookTwo setTitle:@"BarBar"];
if (![managedObjectContext save:&saveError]) {
NSLog(@"Saving changes to book book two failed: %@", saveError);
} else {
// The changes to bookTwo have been persisted.
}
(Note: bookTwomust be a managed object that is associated with managedObjectContextfor this example to work.)
(注意:bookTwo必须是与managedObjectContext此示例相关联的托管对象才能工作。)
回答by Peter Hosey
Sounds like you're thinking in terms of an underlying relational database. Core Data's API is built around model objects, not relational databases.
听起来您正在考虑底层关系数据库。Core Data 的 API 是围绕模型对象构建的,而不是关系数据库。
An entity is a Cocoa object—an instance of NSManagedObjector some subclass of that. The entity's attributes are properties of the object. You use key-value coding or, if you implement a subclass, dot syntax or accessor methods to set those properties.
一个实体是一个 Cocoa 对象——它的一个实例NSManagedObject或它的某个子类。实体的属性是对象的属性。您使用键值编码,或者,如果您实现子类,点语法或访问器方法来设置这些属性。
Evan DiBiase's answer shows one correct way to set the property—specifically, an accessor message. Here's dot syntax:
Evan DiBiase 的回答显示了一种设置属性的正确方法——特别是访问器消息。这是点语法:
bookTwo.title = @"BarBar";
And KVC (which you can use with plain old NSManagedObject):
和 KVC(你可以使用普通的 old NSManagedObject):
[bookTwo setValue:@"BarBar" forKey:@"title"];
回答by Marc Charbonneau
If I'm understanding your question correctly, I think that all you need to keep in mind is managed objects are really no different than any other Cocoa class. Attributes have accessors and mutators you can use in code, through key value coding or through bindings, only in this case they're generated by Core Data. The only trick is you need to manually declare the generated accessors in your class file (if you have one) for your entity if you want to avoid having to use setValue:ForKey:. The documentation describes this in more detail, but the short answer is that you can select your attributes in the data model designer, and choose Copy Obj-C 2.0 Method Declarationsfrom the Design menu.
如果我正确理解您的问题,我认为您需要记住的是托管对象实际上与任何其他 Cocoa 类没有什么不同。属性有访问器和修改器,你可以在代码中使用,通过键值编码或绑定,只有在这种情况下,它们是由 Core Data 生成的。唯一的技巧是,如果您想避免使用 setValue:ForKey:,您需要在您的类文件(如果您有的话)中为您的实体手动声明生成的访问器。该文档对此进行了更详细的描述,但简短的回答是您可以在数据模型设计器中选择您的属性,然后从“设计”菜单中选择“复制 Obj-C 2.0 方法声明”。
回答by NamshanNet
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSArray *temp = [[NSArray alloc]initWithObjects:@"NewWord", nil];
[object setValue:[temp objectAtIndex:0] forKey:@"title"];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
I think this piece of code will give you the idea ;)
我认为这段代码会给你这个想法;)

