核心数据获取[_NSObjectID_48_1 userName]:无法识别的选择器发送到实例 [英] Core Data getting [_NSObjectID_48_1 userName]: unrecognized selector sent to instance
问题描述
A - >> B
我有两个实体A& B,每个实体都是一对多关系。我试图通过分组访问特定的B属性值和所有A实体属性。但是我收到错误,因为 [_ NSObjectID_48_1 userName]
:无法识别的选择器发送到实例当我尝试访问B的关系对象(即a)。
NSManagedObjectContext * context = [[SamCoreDataHelper sharedInstance] managedObjectContext];
NSFetchRequest * fr = [[NSFetchRequest alloc] initWithEntityName:@B];
NSError * error;
NSExpressionDescription * total = [[NSExpressionDescription alloc] init];
[total setExpression:[NSExpression expressionWithFormat:@@ sum.marks]];
[total setName:@total];
[total setExpressionResultType:NSDecimalAttributeType];
[fr setPropertiesToFetch:[NSArray arrayWithObjects:@subject,@a,total,nil]];
[fr setPropertiesToGroupBy:[NSArray arrayWithObjects:@subject,@a,nil]];
[fr setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@a]];
fr.returnsObjectsAsFaults = NO;
[fr setResultType:NSDictionaryResultType];
NSArray * resultArray = [context executeFetchRequest:fr error:& error];
for(NSDictionary * result in resultArray){
NSNumber * total = [result valueForKey:@total];
NSString * subject = [result valueForKey:@subject];
A * a = [result objectForKey:@a];
NSLog(@%@,a.useName); //这里我得到错误为** [_ NSObjectID_48_1 userName]:无法识别的选择器发送到实例**
}
您将收到object id当 NSFetchRequest
的结果设置为 NSDictionaryResultType
时,相对对象的NSManagedObject p>
所以你需要通过对象id来获取它:
A * a =(A *)[managedObjectContext objectWithID:[result valueForKey:@a]];
NSLog(%@,a.userName);
UPD:
fr.returnsObjectsAsFaults = NO;
在这里是无用的,因为返回类型是 NSDictionaryResultType
。
A->>B
I have two entities A&B and each are related as one to many relationship. I am trying to access specific B attribute value and all A entity attributes by grouping. However I am getting error as [_NSObjectID_48_1 userName]
: unrecognized selector sent to instance when I try to access relationship object of B(that is a).
NSManagedObjectContext *context = [[SamCoreDataHelper sharedInstance] managedObjectContext];
NSFetchRequest *fr = [[NSFetchRequest alloc] initWithEntityName:@"B"];
NSError *error;
NSExpressionDescription *total = [[NSExpressionDescription alloc] init];
[total setExpression:[NSExpression expressionWithFormat:@"@sum.marks"]];
[total setName:@"total"];
[total setExpressionResultType:NSDecimalAttributeType];
[fr setPropertiesToFetch:[NSArray arrayWithObjects:@"subject",@"a", total, nil]];
[fr setPropertiesToGroupBy:[NSArray arrayWithObjects:@"subject", @"a", nil]];
[fr setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"a"]];
fr.returnsObjectsAsFaults = NO;
[fr setResultType:NSDictionaryResultType ];
NSArray *resultArray = [context executeFetchRequest:fr error:&error];
for (NSDictionary *result in resultArray) {
NSNumber *total = [result valueForKey:@"total"];
NSString *subject = [result valueForKey:@"subject"];
A *a = [result objectForKey:@"a"];
NSLog(@"%@", a.useName);// Here I am getting error as **[_NSObjectID_48_1 userName]: unrecognized selector sent to instance**
}
You will receive "object id" instead of instance of NSManagedObject
for the relative object when the result of NSFetchRequest
is set to NSDictionaryResultType
.
So you need to fetch it "by object id":
A * a = (A*)[ managedObjectContext objectWithID:[result valueForKey:@"a"]];
NSLog("%@",a.userName );
UPD:
fr.returnsObjectsAsFaults = NO;
is useless here because the return type is NSDictionaryResultType
.
这篇关于核心数据获取[_NSObjectID_48_1 userName]:无法识别的选择器发送到实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!