我有一個CoreData中的整數值的列。在從中檢索結果時,我希望列值減去一個數字。CoreData數學函數
喜歡的東西:columnValue - someNumber(這個數字由用戶輸入)
我知道我可能要使用NSPredicate這一點,但我不知道是否有它的功能和語法。
現在我有的替代方法是迭代所有列值並用'someNumber'進行減法。但我認爲應該有一個更好和有效的方式來做到這一點。
編輯:代碼從@ salo.dm的回答
- (NSDictionary *)myFetchResults {
//Predicate works fine
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:pred1, pred2, nil]];
/*Sort Descroptor - Sorting by 4 columns*/
NSSortDescriptor *sortDesc1 = [NSSortDescriptor sortDescriptorWithKey:@"Column1" ascending:YES];
NSSortDescriptor *sortDesc2 = [NSSortDescriptor sortDescriptorWithKey:@"Column2" ascending:YES];
NSSortDescriptor *sortDesc3 = [NSSortDescriptor sortDescriptorWithKey:@"Column3" ascending:YES];
NSSortDescriptor *sortDesc4 = [NSSortDescriptor sortDescriptorWithKey:@"Column4" ascending:YES];
/*Get Data*/
MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"TableName" inManagedObjectContext:context];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDesc1, sortDesc2, sortDesc3, sortDesc4, nil]];
NSArray *listData = [context executeFetchRequest:fetchRequest error:nil];
/*Create subtract expression*/
NSExpressionDescription *subExp1 = [[NSExpressionDescription alloc] init];
[subExpLatitude setName:@"subtraction1"];
[subExpLatitude setExpression:[NSExpression expressionForFunction:@"from:subtract:"
arguments:[NSArray arrayWithObjects:
[NSExpression expressionForKeyPath:@"Column3"],
[NSExpression expressionForConstantValue:[NSNumber numberWithDouble:someNumber1]],
nil]]];
[subExp1 setExpressionResultType:NSDoubleAttributeType];
NSExpressionDescription *subExp2 = [[NSExpressionDescription alloc] init];
[subExpLongitude setName:@"subtraction2"];
[subExpLongitude setExpression:[NSExpression expressionForFunction:@"from:subtract:"
arguments:[NSArray arrayWithObjects:
[NSExpression expressionForKeyPath:@"Column4"],
[NSExpression expressionForConstantValue:[NSNumber numberWithDouble:someNumber2]],
nil]]];
[subExp2 setExpressionResultType:NSDoubleAttributeType];
/*Get difference data*/
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:subExp1, subExp2, nil]];
NSArray *listDifference = [context executeFetchRequest:fetchRequest error:nil];
NSLog(@"Subtraction 1: %@", [[listDifference objectAtIndex:0] objectForKey:@"subtraction1"]);
NSLog(@"Subtraction 2: %@", [[listDifference objectAtIndex:0] objectForKey:@"subtraction2"]);
NSMutableDictionary *dictResult;
[dictResult setObject:listData forKey:@"Data"]
[dictResult setObject:listDifference forKey:@"Difference"]
return dictResult;
}
編輯:獲取coredata對象
這是行不通的。
只好將其更改爲下面得到它的工作(假設這是正確的方式)
NSExpressionDescription *expEntity = [[NSExpressionDescription alloc] init];
[expEntity setName:@"TableNameEntity"];
[expEntity setExpression:[NSExpression expressionForEvaluatedObject]];
[expEntity setExpressionResultType:NSObjectIDAttributeType];
我加expEntity到setPropertiesToFetch列表。現在我在字典中得到兩個值。
{
TableNameEntity = "0x5e22120 <x-coredata://1A659A52-9321-4ACD-992B-04F20E7BDCED/TableNameEntity/p1640>";
subtractionValue = "-24.13";
}
當我嘗試從字典中檢索並訪問TableNameEntity時,應用程序崩潰。
TableNameEntity *tableEntity = (TableNameEntity *)[dict objectForKey:@"TableNameEntity"];
tableEntity.column1 //This is not the exact code. But this operation crashes with error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSObjectID_48_0 column1]: unrecognized selector sent to instance 0x5e22120'
在這裏,如果你發現,關鍵TableNameEntity值包含在引號中,所以我想它返回一個字符串。
看看你能糾正我做錯了什麼。
我已經嘗試了一種替代方法來獲取字典中的列值。這是(這工作正常)。但我想它不是一個好方法。
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:subExp1, @"column1", @"column2", ... @"columnN" nil]];
感謝您的回答。我有一些疑問。 ---在結果中,我得到一個字典,其中有'列值'和'someNumber'的區別。有沒有辦法讓我得到包含差異和DB對象(行)的結果?截至目前我已編碼獲得兩個結果集(數組)。一個包含數據庫對象,另一個包含差異。我將發佈代碼作爲對問題的修改。 – Sahil
salo.dm:請在問題中選中「編輯:獲取coredata對象」。謝謝! – Sahil
請檢查編輯的回覆。 –