我需要找出核心数据实体的属性的最大值.
我仍然坚定地在Cocoa学习曲线中,这是一个我正在学习的简单测试应用程序.
该应用程序从文本文件中导入财富,并在屏幕上显示一个表格.导入在单独的后台线程中完成.
我在网上找到了这个代码,我试图让它起作用:
- (double)getMaxID
{
NSLog(@"in getMaxID"); // debug
// Use a new moc with the original persistentStoreCoordinator to ensure thread safety
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
[moc setPersistentStoreCoordinator:[[self delegate] persistentStoreCoordinator]];
// Create fetch
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"Fortune" inManagedObjectContext:moc]];
[fetch setResultType:NSDictionaryResultType];
// Expression for Max ID
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"id"];
NSExpression *minExpression = [NSExpression expressionForFunction:@"max:" arguments: [NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"maxID"];
[expressionDescription setExpression:minExpression];
[expressionDescription setExpressionResultType:NSDoubleAttributeType];
[fetch …Run Code Online (Sandbox Code Playgroud)