Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.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 获取核心数据对象属性最大值的简单、可靠的方法?_Objective C_Cocoa_Macos_Core Data - Fatal编程技术网

Objective c 获取核心数据对象属性最大值的简单、可靠的方法?

Objective c 获取核心数据对象属性最大值的简单、可靠的方法?,objective-c,cocoa,macos,core-data,Objective C,Cocoa,Macos,Core Data,有没有一种简单或可靠的方法可以找到核心数据属性的最大值?苹果的系统根本不起作用,而且对于这样一个简单的任务来说,它的长度和复杂程度都是荒谬的。我花了将近一天的时间在这个问题上,还没有找到一个满意的答案。请帮忙 我得到与此问题相同的错误:。和他一样,我也没能找到解决问题的办法 这位提问者认为他解决了同样的问题,但正如其他人所评论的,这里的答案显然是错误的 同样的代码也有问题,但至少询问者实际上没有遇到异常。但他似乎无法让它正常工作,最终使用了另一种解决方案,但没有说明是什么 一个人通过检索排序结果

有没有一种简单或可靠的方法可以找到核心数据属性的最大值?苹果的系统根本不起作用,而且对于这样一个简单的任务来说,它的长度和复杂程度都是荒谬的。我花了将近一天的时间在这个问题上,还没有找到一个满意的答案。请帮忙

我得到与此问题相同的错误:。和他一样,我也没能找到解决问题的办法

这位提问者认为他解决了同样的问题,但正如其他人所评论的,这里的答案显然是错误的

同样的代码也有问题,但至少询问者实际上没有遇到异常。但他似乎无法让它正常工作,最终使用了另一种解决方案,但没有说明是什么


一个人通过检索排序结果并使用最大值来绕过它。不理想!但是,如果我不能很快找到解决方案,我想我也必须这样做,或者重组我的模型或业务规则,或者在我的模型中创建并维护一个MaxValue类来解决这个问题……

有一个名为TestEntity的NSManagedObject子类,它有一个名为tesetID的双重属性

-(double)getNextIndex
{
    NSFetchRequest * request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:@"TestEntity" inManagedObjectContext:self.managedObjectContext]];
    NSError * err = nil;
    NSArray * array = [self.managedObjectContext executeFetchRequest:request error:&err];
    NSNumber * value = [array valueForKeyPath:@"@max.testID"];

    return [value doubleValue]+1;
}
-(void)test
{
 for (int i = 0; i<25 ; i++) {
        TestEntity * entity = [[TestEntity alloc] initWithEntity:[NSEntityDescription entityForName:@"TestEntity" inManagedObjectContext:self.managedObjectContext] insertIntoManagedObjectContext:self.managedObjectContext];
        entity.testID = [NSNumber numberWithDouble:[self getNextIndex]];
        NSLog(@"our testID is set as: %@",entity.testID);
    }

}

我在《核心数据编程指南》的“获取特定值”下找到了这一点。它指的是最低限度,但它回答了您的问题。不像人们希望的那么容易,但也不难理解

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:context];
[request setEntity:entity];
// Specify that the request should return dictionaries.
[request setResultType:NSDictionaryResultType];
// Create an expression for the key path.
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"creationDate"];
// Create an expression to represent the minimum value at the key path 'creationDate'
NSExpression *minExpression = [NSExpression expressionForFunction:@"min:" arguments:[NSArray  arrayWithObject:keyPathExpression]];
// Create an expression description using the minExpression 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:@"minDate"];
[expressionDescription setExpression:minExpression];
[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;
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
if (objects == nil) {
    // Handle the error
}
else {
    if ([objects count] > 0) {
        NSLog(@"Minimum date: %@", [[objects objectAtIndex:0] valueForKey:@"minDate"]);
    }
}



[expressionDescription release];

[request release];

不,我的意思是,例如,最年长的员工的年龄是多少?或者我分配给学生的最伟大的学生是什么?用SQL术语来说,这将非常简单:从studentyou中选择maxstudentID,我在原始问题中提到的解决方案对我不起作用。然而,我又试了一次。我设法避免了这种情况,但当我反复调用max、递增并将其存储在新对象中时,max并不总是返回最新的值。因此,例如,使用StudentID创建obj,获取max StudentID,使用++maxStudentID创建新的obj。10次调用后,每2或3个对象都有相同的StudentID!到目前为止,我找到的最可靠的解决方案是返回经过排序的递减studentID,然后取第一个。好的,我将用一些代码来解决这个问题。然后发布一个答案。好的,谢谢。我通过删除[request setResultType:NSDictionaryResultType]行更新了我为避免该错误所做的操作;谢谢,但这是一个同样的例子,给那些我在最初的帖子中联系到他们的问题的人带来了问题。我也遇到了与这些人相同的问题,无法找到帮助我解决问题的答案。无论如何,我完全同意这并不像人们希望的那么容易:伊姆霍,这些方法的作者应该为他们在这种类型的框架中强加的冗长和反直觉感到尴尬。我还没有机会尝试@grady的答案,因为我找到了解决方法,即使用从排序的降序获取请求返回的第一个对象,然后继续。当我只想要一个对象时,解决方法显然是次优的获取所有对象,因此我打算返回并尝试@grady的答案。这非常好。我想知道使用这种方法与使用限制为1的NSFetchRequest和给定属性的排序描述符相比,是否有任何性能提升。