NSManagedObject实例的值copy
来源:互联网 发布:免费医疗软件 编辑:程序博客网 时间:2024/05/01 00:22
从官网的文档我们知道,实例化NSManagedObject不能像一般的nsobject一样[[NSManagedObject alloc] init],这样运行时会出错,正确的方法是: p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008423}
[NSEntityDescription insertNewObjectForEntityForName:entity inManagedObjectContext:[db managedObjectContext]] 或
[[NSManagedObject alloc] initWithEntity:entity inManagedObjectContext:[db managedObjectContext]]
返回的实例。
如果我们想把一个实列的值copy到另一个实例,如何做呢,我们可以根据属性,一个一个copy,但是代码不灵活,如果我们改了数据库字段,那么代码又要改,我发现了一个通用的代码。
- (NSManagedObject*)copyObject:(NSManagedObject*)object toContext:(NSManagedObjectContext*)moc parent:(NSString*)parentEntity;{ NSString *entityName = [[object entity] name]; NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:moc]; [[self lookup] setObject:newObject forKey:[object objectID]]; NSArray *attKeys = [[[object entity] attributesByName] allKeys]; NSDictionary *attributes = [object dictionaryWithValuesForKeys:attKeys]; [newObject setValuesForKeysWithDictionary:attributes]; id oldDestObject = nil; id temp = nil; NSDictionary *relationships = [[object entity] relationshipsByName]; for (NSString *key in [relationships allKeys]) { NSRelationshipDescription *desc = [relationships valueForKey:key]; NSString *destEntityName = [[desc destinationEntity] name]; if ([destEntityName isEqualToString:parentEntity]) continue; if ([desc isToMany]) { NSMutableSet *newDestSet = [NSMutableSet set]; for (oldDestObject in [object valueForKey:key]) { temp = [[self lookup] objectForKey:[oldDestObject objectID]]; if (!temp) { temp = [self copyObject:oldDestObject toContext:moc parent:entityName]; } [newDestSet addObject:temp]; } [newObject setValue:newDestSet forKey:key]; } else { oldDestObject = [object valueForKey:key]; if (!oldDestObject) continue; temp = [[self lookup] objectForKey:[oldDestObject objectID]]; if (!temp && ![destEntityName isEqualToString:parentEntity]) { temp = [self copyObject:oldDestObject toContext:moc parent:entityName]; } [newObject setValue:temp forKey:key]; } } return newObject;}源码来自:http://pastebin.com/efkji4sy
ebook:http://pragprog.com/book/mzcd/core-data
- NSManagedObject实例的值copy
- NSManagedObject
- iOS中创建一个NSManagedObject的流程
- 【实例】copy别人的网页
- NSArrayController,NSManagedObject
- objc 对象的copy和消息转发的实例
- windows的网络访问认证原则和实例(copy)
- CoreData: error: Failed to call designated initializer on NSManagedObject 的解决方法
- NSEntityDescription和NSManagedObject
- Xcode8+ 新建NSManagedObject subclass
- copy的
- java反射copy对象实例
- 值类型 与引用的 copy
- 子类copy 父类的属性值
- 002_你实现过多线程的Core Data么?NSPersistentStoreCoordinator,NSManagedObjectContext和NSManagedObject中的哪些需要在线程中
- 一个非常的简单的图片copy实例,虽然简单却很实用
- Learning Python 022 调用DOS命令 --- 实例:调用Windows里面的copy命令
- copy:字符串copy程序、泛型编程的copy
- 转载《XAMPP安装和使用教程》
- EBMIDE——表单、报表模板设计
- C语言深度解剖【第一章问题】
- 导入/导出Excel
- MS-VC 使用MAP文件快速定位错误行
- NSManagedObject实例的值copy
- DBMS_XMLSTORE
- C++ - Friend友元能继承吗
- 移植thttpd嵌入式web服务器(转)
- 开发者必备的SQL性能优化基本功
- httpservletrequest cannot be resolved to a type
- SQL Server 2008 定时作业的制定(SQL2005参考此方法)
- Chrome源代码分析之进程和线程模型(四)
- oracle xml处理