Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.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
Core data 使用特定值计算核心数据中的实体_Core Data_Nspredicate_Predicatebuilder - Fatal编程技术网

Core data 使用特定值计算核心数据中的实体

Core data 使用特定值计算核心数据中的实体,core-data,nspredicate,predicatebuilder,Core Data,Nspredicate,Predicatebuilder,我有一个具有某种属性的实体。我的表格已经填充(SQLite表格) 在一个属性(我将调用Attribute1)中,我有一个bool值,在使用我的应用程序时会发生变化 如何返回Attribute1值为YES的实体计数 我已经阅读了“核心数据教程”和“谓词编程指南”,但我不知道如何继续 NSPredicate *predicate= [NSPredicate predicateWithFormat:@"Attribute1 == %@",[NSNumber numberWithBool:YES

我有一个具有某种属性的实体。我的表格已经填充(SQLite表格) 在一个属性(我将调用Attribute1)中,我有一个bool值,在使用我的应用程序时会发生变化

如何返回Attribute1值为YES的实体计数

我已经阅读了“核心数据教程”和“谓词编程指南”,但我不知道如何继续

    NSPredicate *predicate= [NSPredicate predicateWithFormat:@"Attribute1 == %@",[NSNumber numberWithBool:YES]];

我试过了,然后呢?它似乎不起作用。

最好的办法是使用countForFetchRequest方法。设置谓词和fetch请求,但不执行实际的fetch,而是执行countForFetchRequest,如下所示:

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];

NSPredicate *predicate = 
    [NSPredicate predicateWithFormat:@"Attribute1 == %@",
        [NSNumber numberWithBool:YES]];

[request setPredicate:predicate];

NSUInteger count = [myManagedObjectContext countForFetchRequest:request error:nil];
您可以在Apple API文档中找到更多信息:

countForFetchRequest:错误: 返回给定的获取请求在传递给executeFetchRequest时将返回的对象数:错误:

  • (NSInteger)countForFetchRequest:(NSFetchRequest)请求错误:(NSError*)错误 参数 要求 指定提取搜索条件的提取请求。 错误 如果执行fetch时出现问题,则返回时将包含一个描述该问题的NSError实例。 返回值 如果给定的获取请求已传递给executeFetchRequest:error:,则返回的对象数;如果发生错误,则返回的对象数为NSNotFound
可用性 在iOS 3.0及更高版本中提供。 声明于 NSManagedObjectContext.h