gpt4 book ai didi

ios - 使用 NSPredicate Core Data 中的数据库字段执行计算

转载 作者:行者123 更新时间:2023-11-29 12:03:36 24 4
gpt4 key购买 nike

我创建了一个从我的数据库中恢复产品的方法,每个项目都有字段高度、宽度和 percErro(百分比误差),

用户输入要搜索的宽高,eg: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 中对于 NSExpression。在您的情况下,您将需要这样的东西:

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"divide:by:(multiply:by:(height,percErro),100) <= %@ AND divide:by:(multiply:by:(width,percErro),100) <= %@",heightInserted, widthInserted];

关于ios - 使用 NSPredicate Core Data 中的数据库字段执行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35872987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com