如何从Core Data实体及其1:m子级使用JSON
创建NSJSONSerialization
对象?
尝试在将获取请求转换为NSDictionary
之前将获取请求输出到JSON
时,我得到了没有任何关联的纯NSDictionary
,或者将propertiesToFetch
设置为关联时我的应用程序崩溃。
这是我的代码:
NSFetchRequest *request = [NSFetchRequest
fetchRequestWithEntityName:@"ContrSector"];
request.resultType = NSDictionaryResultType;
request.propertiesToFetch = @[@"industries"];
NSError *error;
NSArray *result = [self.managedObjectContext
executeFetchRequest:request error:&error];
正确的方法是什么?
最佳答案
propertiesToFetch
需要NSPropertyDescription
的实例,因此请尝试将@“industries”替换为:
NSDictionary *attributes = [[NSEntityDescription entityForName"@ContrSector" inManagedObjectContext:self.managedObjectContext] relationshipsByName];
NSAttributeDescription *industriesRelationship = [attributes objectForKey:@"industries"];
request.propertiesToFetch = @[industriesRelationship];
编辑:从文档中我没有意识到这是一对多的关系:
属性描述可以代表一对一的属性
关系或表达。属性名称或
关系描述必须与
获取请求的实体。