我正在记录 Core Data
属性“通过”( bool 值)
for (Circuit *object in self.distributionBoard.circuits) {
NSLog(@"Core Data Value = %d", object.passed);
}
这记录很好。计算保存的 bool 值
== 1
次数的最有效方法是什么? ?使用
NSFetchReques
或 NSExpression
到目前为止没有产生预期的结果。看过这里:Core Data sum of all instances attribute和类似的,通常的搜索
最佳答案
由于您的属性是 bool 值,因此您可以使其比该答案中描述的方法简单得多。使用谓词匹配 passed
的值然后获取结果的计数而不是获取的对象。就像是:
NSFetchRequest<Event *> *fetchRequest = MyEntity.fetchRequest;
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"passed = true"];
NSError *error = nil;
NSUInteger count = [self.managedObjectContext countForFetchRequest:fetchRequest error:&error];
然后
count
具有 passed
的实例数是 true
.
关于ios - 核心数据,获取某个 bool 值属性的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53507195/