使用 NSPredicate Core Data 中的数据库字段执行计算
Perform calculations with fields of database in NSPredicate Core Data
我创建了一种从我的数据库中恢复产品的方法,每个项目都有字段高度、宽度和 percErro(百分比误差),
用户输入您要搜索的宽度和高度,例如:15 x 15cm
然后该方法应将兼容的产品与这些编号结合起来,同时考虑到其注册中允许的错误百分比。
假设数据库中的产品是 14 x 14cm,错误百分比是 10%(1.4cm),那么您搜索的产品是 15 x 15cm
在我的查询中,我必须做这样的事情:
[query appendString: [NSString stringWithFormat: @ "(heigthInserted * percErro / 1000) <= (height * percErro / 100)"
[query appendString: [NSString stringWithFormat: @ "AND (widthInserted * percErro / 1000) <= (width * percErro / 100)"
NSPredicate * predicate = [NSPredicate predicateWithFormat: query];
[Request setPredicate: predicate];
这应该给我带来误差百分比不超过10%(1.4厘米)的所有产品
我知道 NSPredicate 不接受数学运算,我怎么能得到计算这个误差百分比的结果?
**
Solved
**
I accepted the answer of pbasdf and conclude that mathematical
operators work in NSPredicate, but must be written according to the
documentation of this link: Documentation
Below my 100% functional method with operators:
-(NSMutableArray*)findWithDimensoes:(NSMutableArray*)formatos :(float)largura :(float)altura{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:TABLE_NAME inManagedObjectContext:managedObjectContext]];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"altura" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"largura" ascending:YES];
NSArray *arraySD = [NSArray arrayWithObjects:sortDescriptor1,sortDescriptor2,nil];
[request setSortDescriptors:arraySD];
[request setFetchLimit:20];
NSMutableString *query = [[NSMutableString alloc] init];
[query appendString:[NSString stringWithFormat:@" (habilitado == true) "]];
if (formatos != nil) {
for (ProdutoDimensao *d in formatos) {
if ([d.id intValue] > 0) {
if (([formatos indexOfObject:d] < formatos.count)) {
[query appendString:@" AND "];
}
[query appendString:[NSString stringWithFormat:@" (produtoDimensao.id == %d) ",[d.id intValue]]];
}
}
}
[query appendString:[NSString stringWithFormat:@" AND altura >= from:subtract:(%lf,multiply:by:(%lf,percErro)) AND altura <= add:to:(%lf,multiply:by:(%lf,percErro)) ",altura,altura,altura,altura]];
[query appendString:[NSString stringWithFormat:@" AND largura >= from:subtract:(%lf,multiply:by:(%lf,percErro)) AND largura <= add:to:(%lf,multiply:by:(%lf,percErro)) ",largura,largura,largura,largura]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:query];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];
return [results mutableCopy];
}
恐怕我不明白你的格式字符串(例如,percErro
可以从比较的两边分解出来)。但是您的最后陈述是错误的: 可以在谓词中使用(基本)数学计算。正确的语法在 documentation for NSExpression
中。在您的情况下,您将需要这样的东西:
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"divide:by:(multiply:by:(height,percErro),100) <= %@ AND divide:by:(multiply:by:(width,percErro),100) <= %@",heightInserted, widthInserted];
我创建了一种从我的数据库中恢复产品的方法,每个项目都有字段高度、宽度和 percErro(百分比误差),
用户输入您要搜索的宽度和高度,例如:15 x 15cm
然后该方法应将兼容的产品与这些编号结合起来,同时考虑到其注册中允许的错误百分比。
假设数据库中的产品是 14 x 14cm,错误百分比是 10%(1.4cm),那么您搜索的产品是 15 x 15cm
在我的查询中,我必须做这样的事情:
[query appendString: [NSString stringWithFormat: @ "(heigthInserted * percErro / 1000) <= (height * percErro / 100)"
[query appendString: [NSString stringWithFormat: @ "AND (widthInserted * percErro / 1000) <= (width * percErro / 100)"
NSPredicate * predicate = [NSPredicate predicateWithFormat: query];
[Request setPredicate: predicate];
这应该给我带来误差百分比不超过10%(1.4厘米)的所有产品
我知道 NSPredicate 不接受数学运算,我怎么能得到计算这个误差百分比的结果?
**
Solved
**
I accepted the answer of pbasdf and conclude that mathematical operators work in NSPredicate, but must be written according to the documentation of this link: Documentation
Below my 100% functional method with operators:
-(NSMutableArray*)findWithDimensoes:(NSMutableArray*)formatos :(float)largura :(float)altura{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:TABLE_NAME inManagedObjectContext:managedObjectContext]];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"altura" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"largura" ascending:YES];
NSArray *arraySD = [NSArray arrayWithObjects:sortDescriptor1,sortDescriptor2,nil];
[request setSortDescriptors:arraySD];
[request setFetchLimit:20];
NSMutableString *query = [[NSMutableString alloc] init];
[query appendString:[NSString stringWithFormat:@" (habilitado == true) "]];
if (formatos != nil) {
for (ProdutoDimensao *d in formatos) {
if ([d.id intValue] > 0) {
if (([formatos indexOfObject:d] < formatos.count)) {
[query appendString:@" AND "];
}
[query appendString:[NSString stringWithFormat:@" (produtoDimensao.id == %d) ",[d.id intValue]]];
}
}
}
[query appendString:[NSString stringWithFormat:@" AND altura >= from:subtract:(%lf,multiply:by:(%lf,percErro)) AND altura <= add:to:(%lf,multiply:by:(%lf,percErro)) ",altura,altura,altura,altura]];
[query appendString:[NSString stringWithFormat:@" AND largura >= from:subtract:(%lf,multiply:by:(%lf,percErro)) AND largura <= add:to:(%lf,multiply:by:(%lf,percErro)) ",largura,largura,largura,largura]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:query];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];
return [results mutableCopy];
}
恐怕我不明白你的格式字符串(例如,percErro
可以从比较的两边分解出来)。但是您的最后陈述是错误的: 可以在谓词中使用(基本)数学计算。正确的语法在 documentation for NSExpression
中。在您的情况下,您将需要这样的东西:
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"divide:by:(multiply:by:(height,percErro),100) <= %@ AND divide:by:(multiply:by:(width,percErro),100) <= %@",heightInserted, widthInserted];