コア データ モデルの説明:
経費追跡アプリケーション用のコア データ モデルがあります。「vendor」、「date」、「amount」などの属性を持つ「Money」という名前の抽象的な親エンティティを作成しました。2 つのサブエンティティ "Expense" と "Income" は、親エンティティ "Money" から継承します。次に、属性を持つ「Category」や「subCategory」などの他のエンティティがあります。現在の合計: データ モデルに 5 つのエンティティ。
サブエンティティ「経費」と「収入」からエンティティ「カテゴリ」への関係があります。エンティティ「カテゴリ」は、エンティティ「サブカテゴリ」と対多関係にあるため、カテゴリは 1 つまたは複数のサブカテゴリを持つことができます。エンティティ「カテゴリ」は、エンティティ「費用」および「収入」と対多の関係にあり、特定のカテゴリの費用または収入が存在する可能性があります。それは今のところ私には理にかなっています。
私はテーブルビューを持っており、NSFetchedResultsController を使用してテーブルビューに費用と収入の混合物を入力しています。
ボタン「ExpenseS」と「Income」を含むアラート ビューがあります。どちらも同じビュー コントローラにプッシュされ、ベンダー、金額、カテゴリ、サブカテゴリ、ナビゲーション バーの [保存] ボタンなどの詳細が表示されます。
それが私の保存方法です:
- (void)saveMoney:(id)sender
{
AppDelegate * applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext * context = [applicationDelegate managedObjectContext];
_money = (Money*) [NSEntityDescription insertNewObjectForEntityForName:@"Money" inManagedObjectContext:context];
double expenseAmt = [_amountTxtField.text doubleValue];
NSDate *expenseDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@",currentDateLbl.text]];
_money.vendor = vendorTxtField.text;
_money.cat = categoryLbl.text;
_money.subcat = subCategoryLbl.text;
_money.amount = [NSNumber numberWithDouble:expenseAmt];
_money.date = expenseDate;
_money.photo = _templateImgView.templateImage.image;
_money.notes = notesLbl.text;
_money.paidBy = paidResourceLbl.text;
NSError * error = nil;
[context save:&error];
[self.navigationController dismissModalViewControllerAnimated:YES];
}
これにより、新しい費用/収入がテーブル ビューに保存され、結果 (費用/収入) の組み合わせがテーブル ビューに表示されます。しかし、自分の列が費用なのか収入なのか、どうすればわかりますか? フェッチリクエストはそれを機能させるべきだと思いますか? しかし、この取得リクエストはどこに、どの方法で配置する必要があるのでしょうか? 次のコードを使用してみました:
AppDelegate * applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] デリゲート]; NSManagedObjectContext * context = [applicationDelegate managedObjectContext];
// Retrieve the entity from the local store -- much like a table in a database
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Money" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
[request setIncludesSubentities:YES];
// Set the sorting -- mandatory, even if you're fetching a single record/object
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release]; sortDescriptors = nil;
[sortDescriptor1 release]; sortDescriptor1 = nil;
NSError * error;
NSArray * objects = [context executeFetchRequest:request error:&error];
for (Money* money in objects)
{
NSLog(@"money class");
if([money isKindOfClass:[Expense class]])
{
NSLog(@"expense class");
}
else
{
}
}
[context save:&error];
これは「経費クラス」を出力することはなく、そこにはまったく行きません。これがどのように機能するのか、どの方法でこのフェッチ要求を配置するのかわかりません。
ここで私を助けてください。感謝します。
ありがとうございました