ฉันสร้างวิธีการกู้คืนผลิตภัณฑ์จากฐานข้อมูลของฉัน แต่ละรายการมีฟิลด์ความสูง ความกว้าง และ percErro (ข้อผิดพลาดเปอร์เซ็นต์)
ผู้ใช้กรอกความกว้างและความสูงที่คุณต้องการค้นหา เช่น 15 x 15ซม
จากนั้นวิธีการควรนำผลิตภัณฑ์ที่เข้ากันได้มาด้วยตัวเลขเหล่านี้ โดยคำนึงถึงเปอร์เซ็นต์ของข้อผิดพลาดที่อนุญาตในการลงทะเบียน
สมมติว่าผลิตภัณฑ์มีฐานข้อมูลขนาด 14 x 14 ซม. และเปอร์เซ็นต์ข้อผิดพลาดคือ 10% (1.4 ซม.) จากนั้นคุณค้นหาผลิตภัณฑ์ที่มีขนาด 15 x 15 ซม.
ในคำถามของฉันฉันต้องทำอะไรแบบนี้:
[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 ไม่ยอมรับการดำเนินการทางคณิตศาสตร์ ฉันจะรับผลลัพธ์จากการคำนวณเปอร์เซ็นต์ของข้อผิดพลาดนี้ได้อย่างไร
**
แก้ไขแล้ว
**
ฉันยอมรับคำตอบของ pbasdf และสรุปว่าตัวดำเนินการทางคณิตศาสตร์ทำงานใน NSPredicate แต่ต้องเขียนตามเอกสารประกอบของลิงก์นี้: เอกสารประกอบ
ต่ำกว่าวิธีการทำงาน 100% ของฉันกับโอเปอเรเตอร์:
-(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];
}