Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 为什么我的获取请求在使用NSExpression max:时会导致异常?_Objective C_Ios_Core Data_Nsfetchrequest - Fatal编程技术网

Objective c 为什么我的获取请求在使用NSExpression max:时会导致异常?

Objective c 为什么我的获取请求在使用NSExpression max:时会导致异常?,objective-c,ios,core-data,nsfetchrequest,Objective C,Ios,Core Data,Nsfetchrequest,我正在执行提取请求,使用NSPredicate从初始选择中删除一些对象,然后使用NSExpression仅检索托管对象的日期属性的最大值。我使用的代码与Apple在其示例()中给出的代码完全相同,但是,这个表达式似乎在某种程度上考虑了我希望最大值作为数组的属性,因此每次执行fetch时,应用程序都会崩溃,出现“无法识别的选择器”异常(尝试将计数消息发送到\uuu NSDate实例)。如果我将属性名称更改为字符串属性,则对于\uuu NSCFString实例或等效实例,我会得到相同的异常),这意味

我正在执行提取请求,使用NSPredicate从初始选择中删除一些对象,然后使用NSExpression仅检索托管对象的日期属性的最大值。我使用的代码与Apple在其示例()中给出的代码完全相同,但是,这个表达式似乎在某种程度上考虑了我希望最大值作为数组的属性,因此每次执行fetch时,应用程序都会崩溃,出现“无法识别的选择器”异常(尝试将计数消息发送到\uuu NSDate实例)。如果我将属性名称更改为字符串属性,则对于\uuu NSCFString实例或等效实例,我会得到相同的异常),这意味着它正在查找对象并使用正确的属性,但不知何故,这个表达式没有得到正确的应用

我的代码是:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"BridgeOpening" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
request.predicate = [NSPredicate predicateWithFormat:@"lastUpdated != nil && bridge.location == %@", location];

// Specify that the request should return dictionaries.
[request setResultType:NSDictionaryResultType];

// Create an expression for the key path.
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"lastUpdated"];

// Create an expression to represent the maximum value at the key path 'creationDate'
NSExpression *maxExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]];

// Create an expression description using the maxExpression and returning a date.
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];

// The name is the key that will be used in the dictionary for the return value.
[expressionDescription setName:@"maxDate"];
[expressionDescription setExpression:maxExpression];
[expressionDescription setExpressionResultType:NSDateAttributeType];

// Set the request's properties to fetch just the property represented by the expressions.
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];

// Execute the fetch.
NSError *error = nil;
NSLog(@"executing fetch");
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
NSLog(@"fetch finished");
if (objects == nil) {
    // Handle the error.
}
else {
    if ([objects count] > 0) {
        NSLog(@"Minimum date: %@", [[objects objectAtIndex:0] valueForKey:@"maxDate"]);
        lastUpdate = [[objects objectAtIndex:0] valueForKey:@"maxDate"];
    }
}
从未到达“fetch finished”日志,因此执行fetch请求肯定会导致异常

有没有人知道为什么这个表达式所做的任何事情似乎都在期待一个NSArray,或者知道我做错了什么? 我应该补充一点,如果我在没有表达式的情况下执行fetch请求(和NSManagedObjectResultType),那么就没有问题,如果我的谓词在应用表达式之前没有结果,那么就没有问题

我认为这可能与中遇到的问题相同,但没有公认的解决方案,原始海报给出的答案也不可接受(使用NSManagedObjectResultType而不是NSDictionaryResultType,我认为这完全绕过了表达式,几乎肯定否定了使用表达式的内存优势

编辑:
最初我忘了说明这一点,但无论我是否在获取请求中使用谓词,我都有同样的问题。

苹果的示例代码在您的链接中没有为获取请求使用谓词,因此如果您不设置
请求,会发生什么情况。谓词
?如果我不使用谓词,则会发生完全相同的情况-但谢谢-我应该这样做劳工处原本已包括在内。