Ios 空结果集的NSExpressionDescription
使用核心数据,我有一个fetch请求,使用表达式获取某个属性的最小值。如果我在请求上设置了一个谓词,导致没有匹配的结果,那么我将获得EXC_BAD_访问权。这是有道理的,因为你不能在NSArray中添加nil来获得结果,但是最好的解决方法是什么 我可以只使用排序顺序和取数限制1,但NSExpressionDescription API中似乎有点疏忽,即在计算表达式之前,如果没有匹配的对象,则无法返回默认结果或空数组Ios 空结果集的NSExpressionDescription,ios,core-data,exc-bad-access,nsfetchrequest,Ios,Core Data,Exc Bad Access,Nsfetchrequest,使用核心数据,我有一个fetch请求,使用表达式获取某个属性的最小值。如果我在请求上设置了一个谓词,导致没有匹配的结果,那么我将获得EXC_BAD_访问权。这是有道理的,因为你不能在NSArray中添加nil来获得结果,但是最好的解决方法是什么 我可以只使用排序顺序和取数限制1,但NSExpressionDescription API中似乎有点疏忽,即在计算表达式之前,如果没有匹配的对象,则无法返回默认结果或空数组 还是我完全错误地使用了EXC\u BAD\u访问,而setPropertiesT
还是我完全错误地使用了EXC\u BAD\u访问,而setPropertiesToFetch在获取请求中使用的NSExpressionDescriptions在这种情况下应该已经表现得很好了?第二段中的解决方法是我必须使用的,所以这似乎只是API的疏忽。今天遇到了类似的情况。将NSFetchRequest resultType设置为NSDictionaryResultType
- (NSDate *)maxDateForEvent:(Event *)event withContext:(NSManagedContext *)context
{
NSExpression *modifiedDateExpression = [NSExpression expressionForKeyPath:@"modifiedDate"];
NSExpression *maxExpression = [NSExpression expressionForFunction:@"max:"
arguments:[NSArray arrayWithObject:modifiedDateExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"maxDate"];
[expressionDescription setExpression:maxExpression];
[expressionDescription setExpressionResultType:NSDateAttributeType];
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
fetch.entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:context];
fetch.propertiesToFetch = [NSArray arrayWithObject:expressionDescription];
fetch.resultType = NSDictionaryResultType;
fetch.predicate = [NSPredicate predicateWithFormat:@"event == %@", event];
NSError *error;
NSArray *array = [context executeFetchRequest:fetch error:&error];
if (!array || ![array count]) {
return [NSDate distantPast];
}
NSDate *date = [[array lastObject] objectForKey:@"maxDate"];
if (!date) {
return [NSDate distantPast];
}
return date;
}
你能发布你的取回请求吗?