CoreDataに整数値の列があります。その結果を取得する際には、列の値を数値で減算します。CoreData数学関数
何かのような:columnValue - someNumber(この番号は、ユーザによって入力される)
私はこのためにNSPredicateを使用する必要があります知っているが、そのための関数や構文があるかどう認識しませんよ。
ここでもう1つは、すべての列の値を繰り返し、「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];
を持っていた私はsetPropertiesToFetchリストにexpEntityを追加しました。今私は辞書の2つの値を取得します。
{
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の値は引用符に含まれているので、私はその文字列として返される推測されます。
私が間違ったことを修正できるかどうかを確認してください。
代替語で辞書の列の値を取得しようとしました。ここ(これはうまくいく)です。しかし、私はそれが良いアプローチではないと思います。
- (NSArray *)myFetchResults
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"myEntity" inManagedObjectContext:myContext];
request.resultType = NSDictionaryResultType;
NSExpressionDescription *subExDescr = [[NSExpressionDescription alloc] init];
[subExDescr setName:@"subtraction"];
[subExDescr setExpression:[NSExpression expressionForFunction:@"subtract:from:"
arguments:[NSArray arrayWithObjects:
[NSExpression expressionForConstantValue:[NSNumber numberWithInt:someNumber]],
[NSExpression expressionForKeyPath:@"myAttribute"],
nil]]];
[subExDescr setExpressionResultType:NSInteger64AttributeType];
request.propertiesToFetch = [NSArray arrayWithObject:subExDescr, nil];
NSError *err = nil;
NSArray *results = [self.moContext executeFetchRequest:request error:&err];
[request release];
[err release];
return results;
}
フェッチ結果は、辞書の配列になります。次のように
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:subExp1, @"column1", @"column2", ... @"columnN" nil]];
お返事ありがとうございます。私はいくつかの疑問を持っています。 ---その結果、私は '列の値'と 'someNumber'の違いを持つ辞書を取得します。違いとDBオブジェクト(行)を含む結果が得られる方法はありますか?現時点では、2つの結果セット(配列)を取得するようにコーディングしました。 1つはDBオブジェクトを含み、もう1つは違いを含みます。質問の編集としてコードを投稿します。 – Sahil
salo.dm:質問の「Edit:Get coredata object」にチェックを入れてください。ありがとう! – Sahil
編集した返信を確認してください。 –